当我尝试将某些数据插入表格时,我收到此错误:
'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)
所以,不知道该怎么做。
答案 0 :(得分:0)
归功于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")
由于某种原因,在连接中指定字符集会产生差异。