当我插入 MySQL 数据库时,为什么会收到此 UnicodeEncodeError 错误?
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 2: ordinal not in range(128)
我将数据库默认值更改为 utf-8,而不是“latin”....但此错误仍然发生。为什么?
这是在 my.cnf 中。我这样做错了吗?我只是希望所有内容都是 UTF-8。
init_connect='SET collation_connection = utf8_general_ci'
init_connect='SET NAMES utf8'
default-character-set=utf8
character-set-server = utf8
collation-server = utf8_general_ci
default-character-set=utf8
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 2: ordinal not in range(128)
I changed my database default to be utf-8, and not "latin"....but this error still occurs. why?
This is in my.cnf. Am I doing this wrong? I just want EVERYTHING TO BE UTF-8.
init_connect='SET collation_connection = utf8_general_ci'
init_connect='SET NAMES utf8'
default-character-set=utf8
character-set-server = utf8
collation-server = utf8_general_ci
default-character-set=utf8
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MySQLdb.connect(read_default_*) 选项不会设置默认字符集中的字符集。您需要明确设置:
或 django 数据库设置中的等效设置。
MySQLdb.connect(read_default_*) options won't set the character set from default-character-set. You will need to set this explicitly:
Or the equivalent setting in your django databases settings.
如果你从 Python 中得到异常,那么它与 MySQL 无关——错误发生在表达式发送到 MySQL 之前。我认为 MySQLdb 驱动程序不处理 unicode。
如果您正在处理原始 MySQLdb 接口,这会有点烦人(像 SQLAlchemy 这样的数据库包装器将为您处理这些东西),但您可能想要创建一个像这样的函数:
If you get an exception from Python then it's nothing to do with MySQL -- the error happens before the expression is sent to MySQL. I would presume that the MySQLdb driver doesn't handle unicode.
If you are dealing with the raw MySQLdb interface this will be somewhat annoying (database wrappers like SQLAlchemy will handle this stuff for you), but you might want to create a function like this: