SQLAlchemy MYSQL查询utf8字符问题

时间:2020-09-03 08:43:09

标签: python mysql sqlalchemy

尝试从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:字符映射到

1 个答案:

答案 0 :(得分:2)

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

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