当我尝试向表中插入某些数据时出现此错误:
'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,idk如何实际标记您)
当我设置连接时,不要这样做:
db = MySQLdb.connect(host="localhost", user="root", passwd="password", db="database")
我应该这样做:
db = MySQLdb.connect(host="localhost", user="root", passwd="password", db="database", charset="utf8mb4")
由于某些原因,在连接中指定字符集会有所不同。
关于mysql - 即使我使用utf8mb4,MYSQL也给我带来了拉丁1编解码器错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49046590/