python3 django连接mysql 数据库
詳情參考:
https://blog.csdn.net/weixin_33127753/article/details/89100552
https://imshusheng.com/python/216.html
?
報(bào)錯(cuò)環(huán)境 python=3.6,django=2.2,PyMySQL=0.9.3
……
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
解決方法:
Django連接MySQL時(shí)默認(rèn)使用MySQLdb驅(qū)動(dòng),但MySQLdb不支持Python3,因此這里將MySQL驅(qū)動(dòng)設(shè)置為pymysql,使用 pip install pymysql 進(jìn)行安裝,然后在工程文件__init__.py添加以下代碼即可。
#安裝pymysql
pip install pymysql
進(jìn)入工程修改
#__init__.py
import pymysql
pymysql.install_as_MySQLdb()
第一種:
django降到2.1.4版本就OK了
第二種(仍使用django 2.2版本):
#找到Python環(huán)境下 django包,并進(jìn)入到backends下的mysql文件夾
cd /opt/anaconda3/envs/envAGC_Mini/lib/python3.6/site-packages/django/db/backends/mysql
#文件列表如下
# 找到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__)
此時(shí)仍會(huì)會(huì)報(bào)錯(cuò),報(bào)錯(cuò)信息如下:
AttributeError: ‘str’ object has no attribute ‘decode’
#找到operations.py文件(46行,版本不同行數(shù)不同哈~自個(gè)兒find一下),將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
OK~ 不再報(bào)錯(cuò)
---------------------
作者:Hayley-L
來(lái)源:CSDN
原文:https://blog.csdn.net/weixin_33127753/article/details/89100552
版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請(qǐng)附上博文鏈接!
轉(zhuǎn)載于:https://www.cnblogs.com/sunmeng1994/p/11171978.html
總結(jié)
以上是生活随笔為你收集整理的python3 django连接mysql 数据库的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 查看JDK版本和安装路径
- 下一篇: linux cmake编译源码,linu