pymysql KeyError:25

发布时间:2019-10-11 09:02:50编辑:auto阅读(2271)

    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 Files\Python36\lib\site-packages\pymysql\__init__.py", line 90, in Connect
        return Connection(*args, **kwargs)
      File "C:\Program Files\Python36\lib\site-packages\pymysql\connections.py", line 706, in __init__
        self.connect()
      File "C:\Program Files\Python36\lib\site-packages\pymysql\connections.py", line 931, in connect
        self._get_server_information()
      File "C:\Program Files\Python36\lib\site-packages\pymysql\connections.py", line 1269, in _get_server_information
        self.server_charset = charset_by_id(lang).name
      File "C:\Program Files\Python36\lib\site-packages\pymysql\charset.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

    相关内容:

关键字