执行pymysql.connect时出现错误keyerror 255

9njqaruj  于 2021-06-18  发布在  Mysql
关注(0)|答案(3)|浏览(456)

这是密码

import pymysql
pymysql.connect(
    host='localhost',
    port=3306,
    user='root',
    password='iDontWannaSay',
    db='iDontWannaShow',
    charset='utf8'
)

错误回溯是:

data is :::::b'\xff\x02\x00\xff\x81\x15'....##### I was add near line 1279 which is print("data is :::::%s...."%data[i:i+6])
Traceback (most recent call last):
  File "C:\Users\123\Desktop\pymysqldebug.py", line 8, in <module>
    charset='utf8'
  File "D:\Program Files (x86)\Python\Python35\lib\site-packages\pymysql\__init__.py", line 90, in Connect
    return Connection(*args,**kwargs)
  File "D:\Program Files (x86)\Python\Python35\lib\site-packages\pymysql\connections.py", line 709, in __init__
    self.connect()
  File "D:\Program Files (x86)\Python\Python35\lib\site-packages\pymysql\connections.py", line 934, in connect
    self._get_server_information()
  File "D:\Program Files (x86)\Python\Python35\lib\site-packages\pymysql\connections.py", line 1279, in _get_server_information
    self.server_charset = charset_by_id(lang).name
  File "D:\Program Files (x86)\Python\Python35\lib\site-packages\pymysql\charset.py", line 39, in by_id
    return self._by_id[id]
KeyError: 255

似乎struct.unpack方法将“\xff\”解析为255并分配给 self.server_language ,无论传递的非null字符集参数是什么。
这是mysql版本问题吗 8.0.1-dmr )

kxxlusnw

kxxlusnw1#

若要扩展上述仅接受答案的链接,请考虑对当前链接进行以下更改 pymysql 安装。对于mysql 8,mysql python api无法识别可能较新的字符集,因此 KeyError .
要解决此问题,请在 pymysql 在此目录中找到模块:

print(pymysql.__file__)

备份original connectors.py脚本。然后合并以下更改。
原件(第1268-1269行)

self.server_language = lang
self.server_charset = charset_by_id(lang).name

更换(第1268-1272行)

self.server_language = lang
try:
    self.server_charset = charset_by_id(lang).name
except KeyError:
    self.server_charset = None

注意在上面的行的缩进中包含隐藏的制表符。
参考
pymysql git补丁#591

y0u0uwnf

y0u0uwnf2#

但是你的变量呢?

import pymysql

    |
    v
myVariable = pymysql.connect(
    host='localhost',
    port=3306,
    user='root',
    password='iDontWannaSay',
    db='iDontWannaShow',
    charset='utf8'
)
daolsyd0

daolsyd03#

这个问题可以参考pull请求591

相关问题