django 使用PyMySQL连接mysql

* 安装pymysql模块

pip install pymysql

 

* settings.py添加下面设置

## pymysql repalce mysqldb
import pymysql
pymysql.install_as_MySQLdb()

 

* mysqlclient 版本太低 

 报错:

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.

解决:

cd {python_root_dir}/Lib\site-packages\django\db\backends\mysql

base.py

# 找到base.py文件,注释掉 base.py 中如下部分(35/36行)

# if version < (1, 3, 3):
#     raise ImproperlyConfigured("mysqlclient 1.3.3 or newer is required; you have %s" % Database.__version__)

 

* 编码问题

报错:

AttributeError: ‘str’ object has no attribute ‘decode’

解决:

cd {python_root_dir}/Lib\site-packages\django\db\backends\mysql

operations.py

#找到operations.py文件(146行),将decode改为encode
#linux vim 查找快捷键:?decode

if query is not None:
    query = query.decode(errors='replace')
return query

#改为
if query is not None:
    query = query.encode(errors='replace')
return query

 

posted on 2019-05-22 17:58  林肯公园  阅读(6243)  评论(0编辑  收藏  举报

导航