Django mysqlclient安装和使用详解


Posted in Python onSeptember 17, 2020

一、安装mysqlclient

网上看到很过通过命令:pip install mysqlclient 进行安装的教程,但是我却始终安装失败,遇到的错误千奇百怪,后来通过自己下载mysqlclient客户端终于安装成功;

首先打开网址:https://www.lfd.uci.edu/~gohlke/pythonlibs/并找到下面图中的内容部分:

Django mysqlclient安装和使用详解

根据自己的需要,我选择的是最下边的cp38(目测cp38应该是C++版本,下载下来的文件通过pip install 进行安装的时候会进行c++编译,如果你的电脑(我是Windows)上没有安装VC++,那么找个新版本的安装一下即可:https://support.microsoft.com/zh-cn/help/2977003/the-latest-supported-visual-c-downloads)记住如果没有C++,就先安装C++这个;

下载好mysqlclientt之后如下(只要下载1个,我系统是64位,所以先下载的64位的,结果用不了,所以又下载了32位的才成功,所以建议先下载32位的试试):

Django mysqlclient安装和使用详解

打开控制台(开始->运行->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

1.进入项目工程目录执行命令:django-admin startapp TcesApp,我的完整命令是:C:\Users\Yeat\PycharmProjects\untitled>django-admin startapp TcesApp,前面的部分是我的工程目录路径;

2.命令执行完毕后工程里会增加TcesApp目录如图:

Django mysqlclient安装和使用详解

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:

Django mysqlclient安装和使用详解

在models.py中可以创建过个表的model。

4.在admin.py中注册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 的名称):

Django mysqlclient安装和使用详解

6.还是在settings.py中修改DATABASES内容如下:

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是你的数据库名称,HOST是数据库地址,其它的大家都知道。

7.接下来我们到views.py(或者自己创建的py文件)中编写代码主要看 addExam 这个方法:

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 + '数据添加成功!'
 return render(request,'helloworld.html',context)

其中helloworld.html是放在templates中的前端页面:

Django mysqlclient安装和使用详解

context['value']就是html页面中的{{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)
]

三、运行效果如下:

Django mysqlclient安装和使用详解

Django mysqlclient安装和使用详解

Django mysqlclient安装和使用详解

 到此这篇关于Django mysqlclient安装和使用详解的文章就介绍到这了,更多相关Django mysqlclient安装使用内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python升级提示Tkinter模块找不到的解决方法
Aug 22 Python
Python字符串处理实例详解
May 18 Python
Python通过matplotlib画双层饼图及环形图简单示例
Dec 15 Python
django将图片上传数据库后在前端显式的方法
May 25 Python
Python 修改列表中的元素方法
Jun 26 Python
python实现小球弹跳效果
May 10 Python
python爬虫之快速对js内容进行破解
Jul 09 Python
自适应线性神经网络Adaline的python实现详解
Sep 30 Python
tensorflow多维张量计算实例
Feb 11 Python
Django Serializer HiddenField隐藏字段实例
Mar 31 Python
解决运行出现'dict' object has no attribute 'has_key'问题
Jul 15 Python
python not运算符的实例用法
Jun 30 Python
Pycharm2020最新激活码|永久激活(附最新激活码和插件的详细教程)
Sep 29 #Python
Django返回HTML文件的实现方法
Sep 17 #Python
Pycharm新手使用教程(图文详解)
Sep 17 #Python
Django修改app名称和数据表迁移方案实现
Sep 17 #Python
Python request中文乱码问题解决方案
Sep 17 #Python
python如何使用腾讯云发送短信
Sep 17 #Python
通俗易懂了解Python装饰器原理
Sep 17 #Python
You might like
网友原创的PHP模板类代码
2008/09/07 PHP
PHP代码优化技巧小结
2015/09/29 PHP
thinkPHP下ueditor的使用方法详解
2015/12/26 PHP
PHP常见漏洞攻击分析
2016/02/21 PHP
php简单截取字符串代码示例
2016/10/19 PHP
PHP基于redis计数器类定义与用法示例
2018/02/08 PHP
PHP之header函数详解
2021/03/02 PHP
javascript通过className来获取元素的简单示例代码
2014/01/10 Javascript
jQuery中children()方法用法实例
2015/01/07 Javascript
微信小程序 页面跳转传参详解
2016/10/28 Javascript
angular2+node.js express打包部署的实战
2017/07/27 Javascript
基于iScroll实现内容滚动效果
2018/03/21 Javascript
JS实现数组的增删改查操作示例
2018/08/29 Javascript
微信小程序实现时间戳格式转换
2020/07/20 Javascript
Python3.0与2.X版本的区别实例分析
2014/08/25 Python
Python实现删除Android工程中的冗余字符串
2015/01/19 Python
python实现从网络下载文件并获得文件大小及类型的方法
2015/04/28 Python
利用python代码写的12306订票代码
2015/12/20 Python
如何用itertools解决无序排列组合的问题
2017/05/18 Python
Python实现的朴素贝叶斯分类器示例
2018/01/06 Python
tensorflow实现KNN识别MNIST
2018/03/12 Python
Tornado Web Server框架编写简易Python服务器
2018/07/28 Python
python 实现得到当前时间偏移day天后的日期方法
2018/12/31 Python
爬虫代理池Python3WebSpider源代码测试过程解析
2019/12/20 Python
Python如何基于Tesseract实现识别文字功能
2020/06/05 Python
详解pandas获取Dataframe元素值的几种方法
2020/06/14 Python
Pytorch 卷积中的 Input Shape用法
2020/06/29 Python
css3 中translate和transition的使用方法
2020/03/26 HTML / CSS
工作表扬信的范文
2014/01/10 职场文书
单位消防安全责任书
2014/07/23 职场文书
纪念九一八事变演讲稿1000字
2014/09/14 职场文书
教师批评与自我批评心得体会
2014/10/16 职场文书
租赁协议书
2015/01/27 职场文书
采购员岗位职责范本
2015/04/07 职场文书
检讨书范文大全
2015/05/07 职场文书
导游词之襄阳古城
2019/09/27 职场文书