1366 mysql 不正确的注音符号字符串值

luc*_*mia 1 python mysql utf-8 mysql-python

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

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

但是我已经将连接字符集和从 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'
Run Code Online (Sandbox Code Playgroud)

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

>>> u'\U0002010e'.encode('utf8')
'\xf0\xa0\x84\x8e'
Run Code Online (Sandbox Code Playgroud)

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

Ign*_*ams 5

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