Python / Mysql俄语INSERT

时间:2012-12-04 20:57:34

标签: python mysql cyrillic

尝试用俄语执行INSERT时遇到以下错误:

sql =   """SELECT provider FROM main_app_provider WHERE provider LIKE %s"""
cursor.execute(sql, args)

[ print statement ]
SELECT provider FROM main_app_provider WHERE provider LIKE Централ%
  ...
  File "/Library/Python/2.7/site-packages/MySQLdb/connections.py", line 202, in unicode_literal
return db.literal(u.encode(unicode_literal.charset))
UnicodeEncodeError: 'latin-1' codec can't encode characters 
in position 0-6: ordinal not in range(256)

我将如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

MySQLdb默认使用latin-1,你必须设置你想要它使用的字符集。 (见这个问题:python encoding problem with mysqldb

我在创建连接时这样做,例如

conn=MySQLdb.connect(hostname, username, password, database, charset='utf8')