前言
Django自带的数据库是sqllite,但是很多大型项目要用到mysql。
正文
和以往版本不同,本次下载的库是PyMySQL
pip3 install PyMySQL
在项目的init.py文件中引入
import pymysql
pymysql.install_as_MySQLdb()
然后进入settings.py,设置DATABASES
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': "mxonline",
'USER': "root",
'PASSWORD': "314159",
'HOST': "127.0.0.1"
}
}
其实改版后配置也不复杂,就是和Python2.7的方式略有不同。
一、Django数据同步过程中遇到的问题:
1、raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
解决办法:C:\Python37\Lib\site-packages\django\db\backends\mysql(python安装目录)打开base.py,注释掉以下内容:
if version < (1, 3, 13):
raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
2、File "C:\Python37\lib\site-packages\django\db\backends\mysql\operations.py", line 146, in last_executed_query
query = query.decode(errors='replace')
AttributeError: 'str' object has no attribute 'decode'
解决办法:打开此文件把146行的decode修改为encode