pymysql KeyError:255

Evan1995發表於2019-02-16

weibospider專案新建所有的表時遇到的錯誤:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/pool.py", line 1122, in _do_get
    return self._pool.get(wait, self._timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/util/queue.py", line 145, in get
    raise Empty
sqlalchemy.util.queue.Empty

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "create_all.py", line 10, in <module>
    create_all_table()
  File "create_all.py", line 7, in create_all_table
    metadata.create_all()
  ...

沒有找到出錯的具體原因,所以只能更換一個引擎,在這裡找到了一個臨時方案:
安裝新的引擎:

 $ pip install mysql-connector-python

使用方法:

 Engine = create_engine(`mysql+mysqlconnector://<USERNAME>:<PASSWD>@<HOSTNAME>:<PORT>/<DBNAME>`)

(2018.5.3更新)
導致上述問題的主要原因可能是KeyError: 255這個錯誤:

Traceback (most recent call last):
  ...
  File "C:Program FilesPython36libsite-packagespymysql\__init__.py", line 90, in Connect
    return Connection(*args, **kwargs)
  File "C:Program FilesPython36libsite-packagespymysqlconnections.py", line 706, in __init__
    self.connect()
  File "C:Program FilesPython36libsite-packagespymysqlconnections.py", line 931, in connect
    self._get_server_information()
  File "C:Program FilesPython36libsite-packagespymysqlconnections.py", line 1269, in _get_server_information
    self.server_charset = charset_by_id(lang).name
  File "C:Program FilesPython36libsite-packagespymysqlcharset.py", line 38, in by_id
    return self._by_id[id]
KeyError: 255

主要原因是MySQL8.0更新了很多字符集,但是這些字符集長度超過255了,所以舊版的PyMySQL不支援長度超過255的字元
檢視當前版本的PyMySQL:

> pip list
Package                Version
---------------------- -----------
PyMySQL                0.7.11

更新PyMySQL:

> pip install --upgrade PyMySQL
Collecting PyMySQL
  Downloading https://files.pythonhosted.org/packages/e5/07/c0f249aa0b7b0517b5843eeab689b9ccc6a6bb0536fc9d95e65901e6f2ac/PyMySQL-0.8.0-py2.py3-none-any.whl (83kB)
    100% |████████████████████████████████| 92kB 453kB/s
Installing collected packages: PyMySQL
  Found existing installation: PyMySQL 0.7.11
    Uninstalling PyMySQL-0.7.11:
      Successfully uninstalled PyMySQL-0.7.11
Successfully installed PyMySQL-0.8.0

相關內容:

相關文章