Pymysql連線資料庫出現版本問題
阿新 • • 發佈:2020-12-25
技術標籤:PythonFAQ
Pymysql連線資料庫出現版本問題
1.問題描述:
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
2.問題原因:
由於MySQLdb不支援Python3,因此將MySQL驅動設定為pymysql,在使用中會出現版本不匹配的問題
3.解決方法:
#找到Python環境下 django包,並進入到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__)
再次執行時若出現如下問題:
注:如果有 mysqlclient 庫的情況下使用 pymysql 庫連結 mysql 就會出現這個問題。
AttributeError: ‘str’ object has no attribute ‘decode’
#找到operations.py檔案(46行,版本不同行數可能不同,需要自行查詢),將decode改為encode
if query is not None:
query = query.decode(errors='replace')
return query
#改為
if query is not None:
query = query.encode(errors='replace')
return query