Python Django mysqlclient安装和使用
一、安装mysqlclient
网上看到很过通过命令:pip install mysqlclient 进行安装的教程,但是我却始终安装失败,遇到的错误千奇百怪,后来通过自己下载mysqlclient客户端终于安装成功;
首先打开网址:https://www.lfd.uci.edu/~gohlke/pythonlibs/#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位的试试):

打开控制台(开始->运行->cmd):
第一步:cd 到下载的mysqlclient文件所在的目录:cd C:\Users\Yeat\Downloads\mysqlclient
第二步:执行安装命令:pip install mysqlclient-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目录如图:

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:
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内容如下:

完整配置:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'tces',
'USER': 'root',
'PASSWORD': 'Unity3du#d112233',
'HOST': 'nas.yeatsoft.com',
'PORT': '',
'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 = ''
exam.SceneID = '',
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中添加路径完整代码如下:
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)
]
三、运行效果如下:



结束!
Python Django mysqlclient安装和使用的更多相关文章
- Python & Django & Pycharm 安装
一.下载安装Python 从https://www.python.org/上下载 Python 2.7.6,双击安装包开始安装: 单击“Next”按钮,进入Python安装组件选择界面.这里我们安装全 ...
- python, Django 的安装
yum install zlib yum install zlib-devel yum install openssl-devel 否则导致安装setuptools出错,还需要重新编译python 如 ...
- Python Django的安装配置
学习Django前,我们要确定电脑上是否已经安装了Python,目前Python有两个版本,不过这两个版本并不互相兼容,所以根据个人选择合适的版本. 因为从Django2.0开始将不再支持Python ...
- python,django安装
环境:win7 64位 软件:python3.4.3,jdango1.8,PyDev,pymysql0.7 一:安装python 1.安装好python好后,配置环境变量,可以参考其它的博客,本博客只 ...
- python Django教程 之 安装、基本命令、视图与网站
python Django教程 之 安装.基本命令.视图与网站 一.简介 Django 中提供了开发网站经常用到的模块,常见的代码都为你写好了,通过减少重复的代码,Django 使你能够专注于 w ...
- Windows上python开发--2安装django框架
Windows上python开发--2安装django框架 分类: 服务器后台开发2014-05-17 21:22 2310人阅读 评论(2) 收藏 举报 python django 上一篇文章中讲了 ...
- python Django 学习笔记(一)—— Django安装
注:本人python版本2.7.5 ,win7系统 安装Django https://www.djangoproject.com/download/ 官方下载Django-1.5.5.tar.gz 1 ...
- 第四百零一节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署virtualenv虚拟环境安装,与Python虚拟环境批量安装模块
第四百零一节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署virtualenv虚拟环境安装,与Python虚拟环境批量安装模块 virtualenv简介 1.安装virtuale ...
- Window环境下Python和Django的安装,以及项目的创建
1.首先我们要下载python和Django,他们的下载地址如下 python地址:https://www.python.org/ Django地址: https://www.djangoproje ...
随机推荐
- MQTT基础概念介绍
https://blog.csdn.net/pipinet123/article/details/60866901 源博客地址:http://blog.csdn.net/pipinet123 MQTT ...
- The Art of Picking Intel Registers Intel寄存器的艺术
https://www.swansontec.com/sregisters.html I wrote this article for an online magazine called Scene ...
- taocrypt
taocrypt MySQL Bugs: #25189: mysqld: coding.cpp:243: void TaoCrypt::Base64Decoder::Decode(): Asserti ...
- Problems with Localtime
http://pytz.sourceforge.net/#problems-with-localtime https://docs.djangoproject.com/en/2.2/topics/i1 ...
- matplotlib显示黑白灰度图像颜色设置
对于黑白灰度图像(矩阵) 1. 默认使用伪彩色拉升 2 cmap参数为 binary,可能导致颜色反转 3. cmap = gray,same color as origin, that is, wh ...
- php 判断图片文件的真实类型
/** * * 检测文件的真实类型 * * @param string $srcPath 文件路径 * * @return string $realType 文件真实类型 * */ $imgurl = ...
- [原][工具][global mapper]查看图元属性(查看shp文件属性值)
常用的shp文件需要查看其内部字段 目前常用的有三种方法: 1.使用excel打开dbf文件,直接查看shp数据库文本文件 2.使用global mapper查看shp图元,然后通过内部工具查看“图元 ...
- 1. Tomcat之startup.sh
#判断操作系统os400=falsecase "`uname`" inOS400*) os400=true;;esac # 取脚本名称PRG="$0" # 判断 ...
- Python数据抓取技术与实战 pdf
Python数据抓取技术与实战 目录 D11章Python基础1.1Python安装1.2安装pip1.3如何查看帮助1.4D1一个实例1.5文件操作1.6循环1.7异常1.8元组1.9列表1.10字 ...
- 触屏Tap模拟事件
触屏的click因为有双击判断所以有200ms的延迟,zepto里的touch.js兼容不好所以tap也没法直接用. gibhub上有个fastclick太大了. 自己用touched写个简单的模拟t ...