0

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)

我将如何解决这个问题?

4

1 回答 1

2

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

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

conn=MySQLdb.connect(hostname, username, password, database, charset='utf8')
于 2012-12-04T21:11:02.580 回答