I have run into the following error when trying to do an INSERT in Russian:
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)
How would I go about fixing this?
Slava Vedenin
60.4k13 gold badges44 silver badges61 bronze badges
asked Dec 4, 2012 at 20:57
David542
112k211 gold badges584 silver badges1.1k bronze badges
-
what is the charset of your table column? is it being represented correctly in DB?Anov– Anov2012年12月04日 21:08:08 +00:00Commented Dec 4, 2012 at 21:08
-
put single quotes around '%s'Andy– Andy2012年12月04日 22:10:51 +00:00Commented Dec 4, 2012 at 22:10
1 Answer 1
MySQLdb defaults to using latin-1, you have to set the character set you want it to use. (See this question: python encoding problem with mysqldb)
I do this when creating a connection, eg
conn=MySQLdb.connect(hostname, username, password, database, charset='utf8')
answered Dec 4, 2012 at 21:11
Anov
2,3212 gold badges20 silver badges26 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
default