【问题标题】:retreive emojii from mysql database从 mysql 数据库中检索图像
【发布时间】:2021-10-07 20:59:27
【问题描述】:

我正在尝试从 Mysql 中选择一个包含表情符号的 utf8 字符串 而不是 emojii 我得到的是问号

import MySQLdb
db=MySQLdb.connect(host='host', user='user', passwd='password', db='db', charset='utf8mb4', use_unicode=True)
c=db.cursor()
c.execute("SET NAMES UTF8")

c.execute("SELECT message FROM chat WHERE...")
res = c.fetchone()[0]
#the res variable should contain '????234абв'. I checked in database explorer

#saving the res into the file to avoid terminal encodeing issues
with open('test.txt','w',encoding='utf-8-sig') as f:
    f.write(res)

#and voila! I'm gettin '?234абв' in the file!!!

我的数据库确实使用带有排序规则“utf8mb4_unicode_ci”的“utf8mb4”编码。 mysql 连接使用 charset='utf8mb4' 选项初始化。 我确实运行了“设置名称 utf8”。

我正在使用 python 3.8.5。

我在这里错过了什么?

【问题讨论】:

  • utf8中的“问号”

标签: python mysql utf-8 emoji utf8mb4


【解决方案1】:

和往常一样,找到解决方案的最佳方法是询问某人。橡皮鸭调试

我发现我应该使用 c.execute("SET NAMES UTF8mb4") 而不是 c.execute("SET NAMES UTF8")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-19
    • 2015-07-06
    • 2013-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多