Djangomysqlclient安装和使⽤详解
⼀、安装mysqlclient
⽹上看到很过通过命令:pip install mysqlclient 进⾏安装的教程,但是我却始终安装失败,遇到的错误千奇百怪,后来通过⾃⼰下载mysqlclient客户端终于安装成功;
下载好mysqlclientt之后如下(只要下载1个,我系统是64位,所以先下载的64位的,结果⽤不了,所以⼜下载了32位的才成功,所以建议先下载32位的试试):
打开控制台(开始->运⾏->cmd):
第⼀步:cd 到下载的mysqlclient⽂件所在的⽬录:cdC:\Users\Yeat\Downloads\mysqlclient
第⼆步:执⾏安装命令:pip installmysqlclient-1.4.4-cp38-cp38-win32.whl
如果成功的话会看到:
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>当然如果失败的话,那很可能看到类似下图的画⾯:
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版本,只能提供这个办法了
⼆、在Django框架⾥使⽤mysql
mysql下载32位1.进⼊项⽬⼯程⽬录执⾏命令:django-admin startapp TcesApp,我的完整命令是:C:\Users\Yeat\PycharmProjects\untitled>django-admin startapp TcesApp,前⾯的部分是我的⼯程⽬录路径;
2.命令执⾏完毕后⼯程⾥会增加TcesApp⽬录如图:
3.进⼊models.py中创建与你的数据库表相对应的对象model,我的内容如下:
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' #数据表名称
我的表结构 e_exams:
在models.py中可以创建过个表的model。
4.在admin.py中注册model:
ib import admin
from . import models
# Register your models here.
ister(models.e_exams)
5.在setting.py中添加app名称(上边的名称 django-admin startapp TcesApp 的名称):
6.还是在settings.py中修改DATABASES内容如下:
完整配置:
DATABASES = {
'default': {
'ENGINE': 'django.sql',
'NAME': 'tces',
'USER': 'root',
'PASSWORD': 'Unity3du#d112233',
'HOST': 'atsoft',
'PORT': '3306',
'OPTIONS': {
"init_command": "SET sql_mode='STRICT_TRANS_TABLES'",
}
}
}
其中NAME是你的数据库名称,HOST是数据库地址,其它的⼤家都知道。
7.接下来我们到views.py(或者⾃⼰创建的py⽂件)中编写代码主要看 addExam 这个⽅法:from django.http import HttpResponse
from django.shortcuts import render
dels 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 + '数据添加成功!'
return render(request,'helloworld.html',context)
其中helloworld.html是放在templates中的前端页⾯:
context['value']就是html页⾯中的{{value}}
8.到urls.py中添加路径完整代码如下:
ib 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)
]
三、运⾏效果如下:
到此这篇关于Django mysqlclient安装和使⽤详解的⽂章就介绍到这了,更多相关Django mysqlclient安装使⽤内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论