gpt4 book ai didi

即使我使用 utf8mb4,MYSQL 也会给我一个 latin-1 编解码器错误

转载 作者:行者123 更新时间:2023-11-29 17:53:54 25 4
gpt4 key购买 nike

当我尝试将某些数据插入表中时,出现此错误:

'latin-1' codec can't encode character '\u2019' in position 8: ordinal not in range(256)

问题是我没有在任何地方使用latin-1 - 表是utf8mb4,排序规则是utf8mb4_unicode_520_ci。为了确保情况确实如此,我检查了:

mysql> SHOW FULL COLUMNS FROM CustomCommands_u1eae585f88c8ab055a227488b2b5adb1;
+-----------------+---------------+------------------------+------+-----+---------+-------+---------------------------------+---------+
| Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
+-----------------+---------------+------------------------+------+-----+---------+-------+---------------------------------+---------+
| command_id | int(10) | NULL | YES | | NULL | | select,insert,update,references | |
| text | varchar(2000) | utf8mb4_unicode_520_ci | YES | | NULL | | select,insert,update,references | |
| contentMetadata | varchar(1000) | utf8mb4_unicode_520_ci | YES | | NULL | | select,insert,update,references | |
| contentType | int(3) | NULL | YES | | NULL | | select,insert,update,references | |
+-----------------+---------------+------------------------+------+-----+---------+-------+---------------------------------+---------+

果然,它绝对是 utf8mb4。这是我用来将数据插入表的 python 代码:

def _add_cc(self, rid, text, contentMetadata, contentType):
query = "INSERT INTO `CustomCommands_%s` (`command_id`,`text`,`contentMetadata`,`contentType`) VALUES (%s,%s,%s,%s)" % (self._mid,'%s','%s','%s','%s')
tup = (rid,text,str(contentMetadata),contentType)
print(query)
print(tup)
self._cur.execute(query, tup)

它打印的查询是:

INSERT INTO `CustomCommands_u1eae585f88c8ab055a227488b2b5adb1` (`command_id`,`text`,`contentMetadata`,`contentType`) VALUES (%s,%s,%s,%s)

元组是:

(1470115915, '@Lil Cap’n Jack ', '{\'MENTION\': \'{"MENTIONEES":[{"M":"u98de557a46645dc6cd7583e538e1ae40","S":"0","E":"15"}]}\'}', 0)

既然一切都是 utf8mb4,我不确定为什么会出现 latin-1 编解码器错误。我什至运行了下面的代码,但它仍然给了我同样的错误。

db = MySQLdb.connect(host="localhost", user="root", passwd="password", db="database")
db.autocommit(True)
cur = db.cursor()

tables = []
cur.execute("SHOW TABLES")
for row in cur.fetchall():
tables.append(row[0])

for table in tables:
cur.execute("ALTER TABLE %s CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci" % table)

所以是的,不知道该怎么做。

最佳答案

归功于Writing UTF-8 String to MySQL with Python以及上面分享它的人(https://stackoverflow.com/users/5871602/daniel-e,不知道如何实际标记您)

当我设置连接时,而不是这样做:

db = MySQLdb.connect(host="localhost", user="root", passwd="password", db="database")

我应该这样做:

db = MySQLdb.connect(host="localhost", user="root", passwd="password", db="database", charset="utf8mb4")

出于某种原因,在连接中指定字符集会产生差异。

关于即使我使用 utf8mb4,MYSQL 也会给我一个 latin-1 编解码器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49046590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com