一、安装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安装和使用的更多相关文章

  1. Python & Django & Pycharm 安装

    一.下载安装Python 从https://www.python.org/上下载 Python 2.7.6,双击安装包开始安装: 单击“Next”按钮,进入Python安装组件选择界面.这里我们安装全 ...

  2. python, Django 的安装

    yum install zlib yum install zlib-devel yum install openssl-devel 否则导致安装setuptools出错,还需要重新编译python 如 ...

  3. Python Django的安装配置

    学习Django前,我们要确定电脑上是否已经安装了Python,目前Python有两个版本,不过这两个版本并不互相兼容,所以根据个人选择合适的版本. 因为从Django2.0开始将不再支持Python ...

  4. python,django安装

    环境:win7 64位 软件:python3.4.3,jdango1.8,PyDev,pymysql0.7 一:安装python 1.安装好python好后,配置环境变量,可以参考其它的博客,本博客只 ...

  5. python Django教程 之 安装、基本命令、视图与网站

    python  Django教程  之 安装.基本命令.视图与网站 一.简介 Django 中提供了开发网站经常用到的模块,常见的代码都为你写好了,通过减少重复的代码,Django 使你能够专注于 w ...

  6. Windows上python开发--2安装django框架

    Windows上python开发--2安装django框架 分类: 服务器后台开发2014-05-17 21:22 2310人阅读 评论(2) 收藏 举报 python django 上一篇文章中讲了 ...

  7. python Django 学习笔记(一)—— Django安装

    注:本人python版本2.7.5 ,win7系统 安装Django https://www.djangoproject.com/download/ 官方下载Django-1.5.5.tar.gz 1 ...

  8. 第四百零一节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署virtualenv虚拟环境安装,与Python虚拟环境批量安装模块

    第四百零一节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署virtualenv虚拟环境安装,与Python虚拟环境批量安装模块 virtualenv简介 1.安装virtuale ...

  9. Window环境下Python和Django的安装,以及项目的创建

    1.首先我们要下载python和Django,他们的下载地址如下 python地址:https://www.python.org/ Django地址:  https://www.djangoproje ...

随机推荐

  1. C语言函数库分类

  2. AndoridSQLite数据库开发基础教程(8)

    AndoridSQLite数据库开发基础教程(8) 添加索引 索引是一种通过预先排序和对表的一个或多个列构建索引表来优化数据库查找的手段.下面为表添加索引,操作步骤如下: (1)在打开的数据库中,单击 ...

  3. Flutter TextField详解

    原文地址:https://www.jianshu.com/p/54419a143d70 实现TextField说简单也简单,说有坑,坑也不小,下面从易到难介绍一下使用 1.最简单的就是无参数调用构造方 ...

  4. visual studio code跳转到定义处插件

    visual studio code 中使用跳转到定义处的插件 https://marketplace.visualstudio.com/items?itemName=Shan.code-settin ...

  5. 深入学习c++--lambda函数

    1. 简单使用 #include <iostream> #include <functional> using namespace std; struct Print { vo ...

  6. 【html】css、js实现网页内容禁止选中

    网页内容不能选中.复制应该如何实现呢? 通过css *{ moz-user-select: -moz-none; -moz-user-select: none; -o-user-select:none ...

  7. DEBUG技巧里的问题1 双击某个变量不能显示

    DEBUG模式  双击 ls_return-type 变量不能显示,提示警告消息 好像说明的不是这个问题, 把字段复制到右边的变量框里可以显示 这个确实有点奇怪了

  8. Spring MVC 设计概述

      MVC设计的根本原因在于解耦各个模块 Spring MVC的架构 对于持久层而言,随着软件发展,迁移数据库的可能性很小,所以在大部分情况下都用不到Hibernate的HQL来满足移植数据库的要求. ...

  9. DBGrid 单击弹出PickList

    type   myGrid = class(TCustomGrid)   end; type   myInplaceEditList = class(TInplaceEditList)   end; ...

  10. realloc(void *__ptr, size_t __size)

    #include <stdlib.h>realloc(void *__ptr, size_t __size):更改已经配置的内存空间,即更改由malloc()函数分配的内存空间的大小.如果 ...