久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Django mysqlclient安裝和使用詳解

瀏覽:29日期:2023-10-18 09:38:37

一、安裝mysqlclient

網(wǎng)上看到很過(guò)通過(guò)命令:pip install mysqlclient 進(jìn)行安裝的教程,但是我卻始終安裝失敗,遇到的錯(cuò)誤千奇百怪,后來(lái)通過(guò)自己下載mysqlclient客戶端終于安裝成功;

首先打開網(wǎng)址:https://www.lfd.uci.edu/~gohlke/pythonlibs/并找到下面圖中的內(nèi)容部分:

Django mysqlclient安裝和使用詳解

根據(jù)自己的需要,我選擇的是最下邊的cp38(目測(cè)cp38應(yīng)該是C++版本,下載下來(lái)的文件通過(guò)pip install 進(jìn)行安裝的時(shí)候會(huì)進(jìn)行c++編譯,如果你的電腦(我是Windows)上沒有安裝VC++,那么找個(gè)新版本的安裝一下即可:https://support.microsoft.com/zh-cn/help/2977003/the-latest-supported-visual-c-downloads)記住如果沒有C++,就先安裝C++這個(gè);

下載好mysqlclientt之后如下(只要下載1個(gè),我系統(tǒng)是64位,所以先下載的64位的,結(jié)果用不了,所以又下載了32位的才成功,所以建議先下載32位的試試):

Django mysqlclient安裝和使用詳解

打開控制臺(tái)(開始->運(yùn)行->cmd):

第一步:cd 到下載的mysqlclient文件所在的目錄:cdC:UsersYeatDownloadsmysqlclient

第二步:執(zhí)行安裝命令:pip installmysqlclient-1.4.4-cp38-cp38-win32.whl

如果成功的話會(huì)看到:

C:UsersYeatDownloads>pip install mysqlclient-1.4.4-cp38-cp38-win32.whlProcessing c:usersyeatdownloadsmysqlclient-1.4.4-cp38-cp38-win32.whlInstalling collected packages: mysqlclientSuccessfully installed mysqlclient-1.4.4

C:UsersYeatDownloads>當(dāng)然如果失敗的話,那很可能看到類似下圖的畫面:

C:UsersYeat>pip install mysqlclient‑1.3.13‑cp36‑cp36m‑win_amd64.whlWARNING: Requirement ’mysqlclient‑1.3.13‑cp36‑cp36m‑win_amd64.whl’ looks like a filename, but the file does not existERROR: mysqlclient‑1.3.13‑cp36‑cp36m‑win_amd64.whl is not a valid wheel filename.

C:UsersYeat>pip install MySQL_python‑1.2.5‑cp27‑none‑win_amd64.whlWARNING: Requirement ’MySQL_python‑1.2.5‑cp27‑none‑win_amd64.whl’ looks like a filename, but the file does not existERROR: MySQL_python‑1.2.5‑cp27‑none‑win_amd64.whl is not a valid wheel filename.

C:UsersYeat>pip install MySQL_python‑1.2.5‑cp27‑none‑win_amd64ERROR: Invalid requirement: ’MySQL_python‑1.2.5‑cp27‑none‑win_amd64’

C:UsersYeat>cd C:UsersYeatDownloads

C:UsersYeatDownloads>pip install MySQL_python-1.2.5-cp27-none-win_amd64.whlERROR: MySQL_python-1.2.5-cp27-none-win_amd64.whl is not a supported wheel on this platform.

C:UsersYeatDownloads>pip install mysqlclient-1.4.4-cp38-cp38-win_amd64.whlERROR: mysqlclient-1.4.4-cp38-cp38-win_amd64.whl is not a supported wheel on this platform.

失敗,那就換下載的mysqlclient版本,只能提供這個(gè)辦法了?。。?!

二、在Django框架里使用mysql

1.進(jìn)入項(xiàng)目工程目錄執(zhí)行命令:django-admin startapp TcesApp,我的完整命令是:C:UsersYeatPycharmProjectsuntitled>django-admin startapp TcesApp,前面的部分是我的工程目錄路徑;

2.命令執(zhí)行完畢后工程里會(huì)增加TcesApp目錄如圖:

Django mysqlclient安裝和使用詳解

3.進(jìn)入models.py中創(chuàng)建與你的數(shù)據(jù)庫(kù)表相對(duì)應(yīng)的對(duì)象model,我的內(nèi)容如下:

from django.db import modelsclass e_exams(models.Model): ID = models.CharField(max_length=50), ExamName = models.CharField(max_length=50) ExamCode = models.CharField(max_length=50) SceneID = models.CharField(max_length=50) Creater = models.CharField(max_length=50) CreateTime = models.DateTimeField() State = models.CharField(max_length=50) Field_Char1 = models.CharField(max_length=50) Field_Char2 = models.CharField(max_length=50) Field_Char3 = models.CharField(max_length=50) class Meta: db_table = ’e_exams’ #數(shù)據(jù)表名稱

我的表結(jié)構(gòu) e_exams:

Django mysqlclient安裝和使用詳解

在models.py中可以創(chuàng)建過(guò)個(gè)表的model。

4.在admin.py中注冊(cè)model:

from django.contrib import adminfrom . import models# Register your models here.admin.site.register(models.e_exams)

5.在setting.py中添加app名稱(上邊的名稱 django-admin startapp TcesApp 的名稱):

Django mysqlclient安裝和使用詳解

6.還是在settings.py中修改DATABASES內(nèi)容如下:

Django mysqlclient安裝和使用詳解

完整配置:

DATABASES = { ’default’: { ’ENGINE’: ’django.db.backends.mysql’, ’NAME’: ’tces’, ’USER’: ’root’, ’PASSWORD’: ’Unity3du#d112233’, ’HOST’: ’nas.yeatsoft.com’, ’PORT’: ’3306’, ’OPTIONS’: { 'init_command': 'SET sql_mode=’STRICT_TRANS_TABLES’', } }}

其中NAME是你的數(shù)據(jù)庫(kù)名稱,HOST是數(shù)據(jù)庫(kù)地址,其它的大家都知道。

7.接下來(lái)我們到views.py(或者自己創(chuàng)建的py文件)中編寫代碼主要看 addExam 這個(gè)方法:

from django.http import HttpResponsefrom django.shortcuts import renderfrom TcesApp.models import e_examsdef hello(request): return HttpResponse(’home page!’)def helloworld(request): context = {} context[’value’] = ’hello world!’ return render(request, ’helloworld.html’, context)def addExam(request): exam = e_exams() exam.ID = ’100001’ exam.SceneID = ’1001’, exam.ExamName = ’期末考試’ exam.save() context = {} context[’value’] = exam.ExamName + ’數(shù)據(jù)添加成功!’ return render(request,’helloworld.html’,context)

其中helloworld.html是放在templates中的前端頁(yè)面:

Django mysqlclient安裝和使用詳解

context[’value’]就是html頁(yè)面中的{{value}}

8.到urls.py中添加路徑完整代碼如下:

from django.contrib import adminfrom django.urls import pathfrom . import homeurlpatterns = [ path(’admin/’, admin.site.urls), path(’home/’, home.hello), path(’helloworld/’, home.helloworld), path(’add/’,home.addExam)]

三、運(yùn)行效果如下:

Django mysqlclient安裝和使用詳解

Django mysqlclient安裝和使用詳解

Django mysqlclient安裝和使用詳解

到此這篇關(guān)于Django mysqlclient安裝和使用詳解的文章就介紹到這了,更多相關(guān)Django mysqlclient安裝使用內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: MySQL 數(shù)據(jù)庫(kù)
相關(guān)文章:
主站蜘蛛池模板: 欧美性福| 91精品国产欧美一区二区成人 | 亚洲精品中文字幕在线观看 | 久久精品国产v日韩v亚洲 | 午夜国产精品视频 | 日韩一区二区不卡 | 亚洲最新中文字幕 | 99免费视频 | 日本成人片网站 | 日韩精品免费在线视频 | 欧美黄色片免费观看 | 一级免费毛片 | www中文字幕| 中文字幕在线视频一区 | 国产毛片毛片 | 日日干天天干 | 成人精品一区二区三区中文字幕 | 精品一区二区免费视频 | 欧美暴操 | 日日夜夜天天干干 | 国产精品自拍视频网站 | av手机在线电影 | 亚洲高清在线观看视频 | 中文字幕在线观看精品视频 | 日日摸天天爽天天爽视频 | 日本三级做a全过程在线观看 | 国产一区二区日韩 | 欧美多人在线 | 直接看av的网站 | 天天影视网色香欲综合网无拦截 | 亚洲在线一区二区 | 天天天干天天射天天天操 | 欧洲视频一区二区 | 黄色网亚洲 | 二区免费视频 | 成av在线 | 国产日韩一区 | 亚洲人成人一区二区在线观看 | 久久久久9999国产精品 | 亚洲a人| 天天干人人干 |