0

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
2
  • what is the charset of your table column? is it being represented correctly in DB? Commented Dec 4, 2012 at 21:08
  • put single quotes around '%s' Commented Dec 4, 2012 at 22:10

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.