当前位置: 移动技术网 > IT编程>数据库>Mysql > Django mysqlclient安装和使用详解

Django mysqlclient安装和使用详解

2020年09月18日  | 移动技术网IT编程  | 我要评论
一、安装mysqlclient网上看到很过通过命令:pip install mysqlclient 进行安装的教程,但是我却始终安装失败,遇到的错误千奇百怪,后来通过自己下载mysqlclient客户

一、安装mysqlclient

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

首先打开网址:并找到下面图中的内容部分:

根据自己的需要,我选择的是最下边的cp38(目测cp38应该是c++版本,下载下来的文件通过pip install 进行安装的时候会进行c++编译,如果你的电脑(我是windows)上没有安装vc++,那么找个新版本的安装一下即可:)记住如果没有c++,就先安装c++这个;

下载好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

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': '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中的前端页面:

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安装使用内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网