Django mysqlclient安裝和使用詳解
一、安裝mysqlclient
網(wǎng)上看到很過(guò)通過(guò)命令:pip install mysqlclient 進(jìn)行安裝的教程,但是我卻始終安裝失敗,遇到的錯(cuò)誤千奇百怪,后來(lái)通過(guò)自己下載mysqlclient客戶端終于安裝成功;
首先打開(kāi)網(wǎng)址:https://www.lfd.uci.edu/~gohlke/pythonlibs/并找到下面圖中的內(nèi)容部分:
根據(jù)自己的需要,我選擇的是最下邊的cp38(目測(cè)cp38應(yīng)該是C++版本,下載下來(lái)的文件通過(guò)pip install 進(jìn)行安裝的時(shí)候會(huì)進(jìn)行c++編譯,如果你的電腦(我是Windows)上沒(méi)有安裝VC++,那么找個(gè)新版本的安裝一下即可:https://support.microsoft.com/zh-cn/help/2977003/the-latest-supported-visual-c-downloads)記住如果沒(méi)有C++,就先安裝C++這個(gè);
下載好mysqlclientt之后如下(只要下載1個(gè),我系統(tǒng)是64位,所以先下載的64位的,結(jié)果用不了,所以又下載了32位的才成功,所以建議先下載32位的試試):
打開(kāi)控制臺(tái)(開(kāi)始->運(yùn)行->cmd):
第一步:cd 到下載的mysqlclient文件所在的目錄:cdC:\Users\Yeat\Downloads\mysqlclient
第二步:執(zhí)行安裝命令:pip installmysqlclient-1.4.4-cp38-cp38-win32.whl
如果成功的話會(huì)看到:
C:\Users\Yeat\Downloads>pip install mysqlclient-1.4.4-cp38-cp38-win32.whl
Processing c:\users\yeat\downloads\mysqlclient-1.4.4-cp38-cp38-win32.whl
Installing collected packages: mysqlclient
Successfully installed mysqlclient-1.4.4
C:\Users\Yeat\Downloads>當(dāng)然如果失敗的話,那很可能看到類似下圖的畫面:
C:\Users\Yeat>pip install mysqlclient‑1.3.13‑cp36‑cp36m‑win_amd64.whl
WARNING: Requirement 'mysqlclient‑1.3.13‑cp36‑cp36m‑win_amd64.whl' looks like a filename, but the file does not exist
ERROR: mysqlclient‑1.3.13‑cp36‑cp36m‑win_amd64.whl is not a valid wheel filename.C:\Users\Yeat>pip install MySQL_python‑1.2.5‑cp27‑none‑win_amd64.whl
WARNING: Requirement 'MySQL_python‑1.2.5‑cp27‑none‑win_amd64.whl' looks like a filename, but the file does not exist
ERROR: MySQL_python‑1.2.5‑cp27‑none‑win_amd64.whl is not a valid wheel filename.C:\Users\Yeat>pip install MySQL_python‑1.2.5‑cp27‑none‑win_amd64
ERROR: Invalid requirement: 'MySQL_python‑1.2.5‑cp27‑none‑win_amd64'C:\Users\Yeat>cd C:\Users\Yeat\Downloads
C:\Users\Yeat\Downloads>pip install MySQL_python-1.2.5-cp27-none-win_amd64.whl
ERROR: MySQL_python-1.2.5-cp27-none-win_amd64.whl is not a supported wheel on this platform.C:\Users\Yeat\Downloads>pip install mysqlclient-1.4.4-cp38-cp38-win_amd64.whl
ERROR: mysqlclient-1.4.4-cp38-cp38-win_amd64.whl is not a supported wheel on this platform.
失敗,那就換下載的mysqlclient版本,只能提供這個(gè)辦法了?。。。?/p>
二、在Django框架里使用mysql
1.進(jìn)入項(xiàng)目工程目錄執(zhí)行命令:django-admin startapp TcesApp,我的完整命令是:C:\Users\Yeat\PycharmProjects\untitled>django-admin startapp TcesApp,前面的部分是我的工程目錄路徑;
2.命令執(zhí)行完畢后工程里會(huì)增加TcesApp目錄如圖:
3.進(jìn)入models.py中創(chuàng)建與你的數(shù)據(jù)庫(kù)表相對(duì)應(yīng)的對(duì)象model,我的內(nèi)容如下:
from django.db import models class 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:
在models.py中可以創(chuàng)建過(guò)個(gè)表的model。
4.在admin.py中注冊(cè)model:
from django.contrib import admin from . import models # Register your models here. admin.site.register(models.e_exams)
5.在setting.py中添加app名稱(上邊的名稱 django-admin startapp TcesApp 的名稱):
6.還是在settings.py中修改DATABASES內(nèi)容如下:
完整配置:
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 HttpResponse from django.shortcuts import render from TcesApp.models import e_exams def 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è)面:
context['value']就是html頁(yè)面中的{{value}}
8.到urls.py中添加路徑完整代碼如下:
from django.contrib import admin from django.urls import path from . import home urlpatterns = [ path('admin/', admin.site.urls), path('home/', home.hello), path('helloworld/', home.helloworld), path('add/',home.addExam) ]
三、運(yùn)行效果如下:
到此這篇關(guān)于Django mysqlclient安裝和使用詳解的文章就介紹到這了,更多相關(guān)Django mysqlclient安裝使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 解決django 向mysql中寫入中文字符出錯(cuò)的問(wèn)題
- Python+Django+MySQL實(shí)現(xiàn)基于Web版的增刪改查的示例代碼
- Django中從mysql數(shù)據(jù)庫(kù)中獲取數(shù)據(jù)傳到echarts方式
- django2.2 和 PyMySQL版本兼容問(wèn)題
- django連接mysql數(shù)據(jù)庫(kù)及建表操作實(shí)例詳解
- python+Django+pycharm+mysql 搭建首個(gè)web項(xiàng)目詳解
- 詳解centos7+django+python3+mysql+阿里云部署項(xiàng)目全流程
- Django執(zhí)行源生mysql語(yǔ)句實(shí)現(xiàn)過(guò)程解析
相關(guān)文章
Python實(shí)現(xiàn)生成隨機(jī)日期字符串的方法示例
這篇文章主要介紹了Python實(shí)現(xiàn)生成隨機(jī)日期字符串的方法,涉及Python日期時(shí)間及隨機(jī)數(shù)運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2017-12-12使用Python將Mysql的查詢數(shù)據(jù)導(dǎo)出到文件的方法
今天小編就為大家分享一篇關(guān)于使用Python將Mysql的查詢數(shù)據(jù)導(dǎo)出到文件的方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-02-02Python自動(dòng)化辦公之生成PDF報(bào)告詳解
因?yàn)楣ぷ餍枰?jīng)常需要生成很多的PDF報(bào)告給客戶查看產(chǎn)品效果以及過(guò)程的講解,每次都需要按照一定的格式的編寫文檔并生成PDF報(bào)告,這樣重復(fù)性的工作實(shí)在太累。本文就來(lái)用Python實(shí)現(xiàn)自動(dòng)生成PDF報(bào)告吧2023-03-03Python使用bar繪制堆積/帶誤差棒柱形圖的實(shí)現(xiàn)
本文先講解bar參數(shù)如何使用,然后分別演示堆積柱形圖和帶誤差柱形圖畫法。具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09Python將文字轉(zhuǎn)成語(yǔ)音并讀出來(lái)的實(shí)例詳解
今天小編就為大家分享一篇Python將文字轉(zhuǎn)成語(yǔ)音并讀出來(lái)的實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07Flask框架踩坑之a(chǎn)jax跨域請(qǐng)求實(shí)現(xiàn)
這篇文章主要介紹了Flask框架踩坑之a(chǎn)jax跨域請(qǐng)求實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02