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搭建Django应用程序步骤及版本冲突问题解决
Nov 19 Python
python中json格式数据输出的简单实现方法
Oct 31 Python
Tensorflow卷积神经网络实例
May 24 Python
Python使用combinations实现排列组合的方法
Nov 13 Python
Python求一批字符串的最长公共前缀算法示例
Mar 02 Python
Python for循环及基础用法详解
Nov 08 Python
运行tensorflow python程序,限制对GPU和CPU的占用操作
Feb 06 Python
Python利用 utf-8-sig 编码格式解决写入 csv 文件乱码问题
Feb 21 Python
python 中不同包 类 方法 之间的调用详解
Mar 09 Python
Pygame的程序开始示例代码
May 07 Python
浅谈django不使用restframework自定义接口与使用的区别
Jul 15 Python
python中mongodb包操作数据库
Apr 19 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提示Deprecated: mysql_connect(): The mysql extension is deprecated的解决方法
2014/08/28 PHP
PHP单例模式详细介绍
2015/07/01 PHP
PHP实现的线索二叉树及二叉树遍历方法详解
2016/04/25 PHP
PHP通过CURL实现定时任务的图片抓取功能示例
2016/10/03 PHP
PHP创建XML的方法示例【基于DOMDocument类及SimpleXMLElement类】
2019/09/10 PHP
关于include标签导致js路径找不到的问题分析及解决
2013/07/09 Javascript
JQuery 文本框回车跳到下一个文本框示例代码
2013/08/30 Javascript
让input框实现类似百度的搜索提示(基于jquery事件监听)
2014/01/31 Javascript
jquery基础教程之数组使用详解
2014/03/10 Javascript
Js操作树节点自动折叠展开的几种方法
2014/05/05 Javascript
简述Jquery与DOM对象
2015/07/10 Javascript
nodejs修复ipa处理过的png图片
2016/02/17 NodeJs
原生JS和jQuery版实现文件上传功能
2016/04/18 Javascript
js表单登陆验证示例
2016/10/19 Javascript
jQuery Form表单取值的方法
2017/01/11 Javascript
ES6新特性二:Iterator(遍历器)和for-of循环详解
2017/04/20 Javascript
AngularJS 最常用的八种功能(基础知识)
2017/06/26 Javascript
利用jQuery异步上传文件的插件用法详解
2017/07/19 jQuery
详解JS中的this、apply、call、bind(经典面试题)
2017/09/19 Javascript
微信小程序实现的贪吃蛇游戏【附源码下载】
2018/01/03 Javascript
Koa日志中间件封装开发详解
2019/03/09 Javascript
vue iview多张图片大图预览、缩放翻转
2019/07/13 Javascript
JS箭头函数和常规函数之间的区别实例分析【 5 个区别】
2020/05/27 Javascript
详解JS深拷贝与浅拷贝
2020/08/04 Javascript
PyCharm搭建Spark开发环境的实现步骤
2019/09/05 Python
Python箱型图绘制与特征值获取过程解析
2019/10/22 Python
django中media媒体路径设置的步骤
2019/11/15 Python
Python ORM框架Peewee用法详解
2020/04/29 Python
Python实现淘宝秒杀功能的示例代码
2021/01/19 Python
英国最大的宠物商店:Pets at Home
2019/04/17 全球购物
初中生三年学习生活的自我评价
2013/11/03 职场文书
大学生职业生涯规划书前言
2014/01/09 职场文书
高中打架检讨书
2014/02/13 职场文书
文明寄语大全
2014/04/11 职场文书
2015年综治维稳工作总结
2015/04/07 职场文书
利用uni-app生成微信小程序的踩坑记录
2022/04/05 Javascript