1

从 MySQLdb 引发“不正确的字符串值错误”。

_mysql_exceptions.OperationalError: (1366, "Incorrect string value: '\\xF0\\xA0\
\x84\\x8E\\xE2\\x8B...' for column 'from_url' at row 1")

但是我已经将连接字符集和从 url 编码设置为 utf8。对于以前的数百万条记录,它可以毫无问题地工作。

会导致异常的值:我认为这个问题与特殊字符 u'\U0002010e'(中文特殊字符“ㄋ”)有关

u'http://www.ettoday.net/news/20120227/27879.htm?fb_action_ids=305328666231772&
fb_action_types=og.likes&fb_source=aggregation&fb_aggregation_id=288381481237582
 \u7c89\u53ef\u611b\U0002010e\u22ef http://www.ettoday.net/news/20120221/26254.h
tm?fb_action_ids=305330026231636&fb_action_types=og.likes&fb_source=aggregation&
fb_aggregation_id=288381481237582 \u597d\u840c\u53c8\u22ef'

但是这个字符也可以在 python 中编码为 utf8。

>>> u'\U0002010e'.encode('utf8')
'\xf0\xa0\x84\x8e'

那么为什么 MySQL 不能接受这个字符呢?

4

2 回答 2

5

您使用的字符在 BMP 之外,因此需要4个字节来存储。使用utf8字符集是不够的;您必须拥有 MySQL 5.5 或更高版本,并改用utf8mb4字符集。

于 2012-09-06T02:30:06.277 回答
0

检查您为 mysql 设置的字符集编码,并确保您使用的是接受 utf8 编码的字符集

于 2012-09-06T02:29:06.260 回答