【问题标题】:SQLAlchemy MYSQL query utf8 character problemSQLAlchemy MYSQL查询utf8字符问题
【发布时间】:2020-12-22 11:26:43
【问题描述】:

尝试从 mysql (charset:utf8) 表中查询所有项目,该表有一个字段,其中包含带有中文和其他特殊字符的行我遇到上述错误

items = session.query(Item).all()

文件 "/root/.local/share/virtualenvs/server-WesSANjA/lib/python3.8/site-packages/MySQLdb/cursors.py", 第 355 行,在 _post_get_result self._rows = self._fetch_row(0) 文件中 "/root/.local/share/virtualenvs/server-WesSANjA/lib/python3.8/site-packages/MySQLdb/cursors.py", 第 328 行,在 _fetch_row 返回 self._result.fetch_row(size, self._fetch_type) 文件 "/usr/local/lib/python3.8/encodings/cp1252.py", 第 15 行,在解码返回中 codecs.charmap_decode(input,errors,decoding_table)

UnicodeDecodeError:“charmap”编解码器无法解码位置 30 中的字节 0x9d:字符映射到

【问题讨论】:

  • 您是否按照建议的here?charset=utf8mb4 附加到您的连接URI?
  • @GordThompson 是的,现在它起作用了!

标签: python mysql sqlalchemy


【解决方案1】:

如文档here 中所述,我们需要使用?charset=utf8mb4 来获得完整的Unicode 支持(包括表情符号):

e = create_engine(
    "mysql+pymysql://scott:tiger@localhost/test?charset=utf8mb4")

【讨论】:

    猜你喜欢
    • 2011-07-12
    • 2013-05-20
    • 2015-11-23
    • 2011-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-17
    相关资源
    最近更新 更多