Python, MySQLdb and UTF-8
Monday, December 17, 2007
So, after waisting way too much time with UTF-8 and MySQLdb, I've finally found a solutution, which seems to work with the newest version of MySQLdb. You maybe also know this problem with the following error message:"UnicodeEncodeError:'latin-1' codec can't encode character ..."
This is because MySQLdb normally tries to encode everythin to latin-1. This can be fixed by executing the following commands right after you've etablished the connection:
"db" is the result of MySQLdb.connect, and "dbc" is the result of db.cursor().db.set_character_set('utf8')dbc.execute('SET NAMES utf8;')dbc.execute('SET CHARACTER SET utf8;')dbc.execute('SET character_set_connection=utf8;')
I hope this will help some guys out, not waisting as much time as I did for this issue.
Comments to this article
Leave a comment
Please note that your email address will not be shown, it is only used to fetch your avatar image from gravatar.com and for notifications.


I've also wasted a long time trying to fix this!
Thanks
thank you for sharing!
Thanks very Much!
This tip helped me a lot in my Graduation project. Please, visit my site to see the results soon, when I publish it.
Best regards,
Thanks! Your recipe has worked perfectly for me :)
You're the fucking master!!!!
Our project owes you a few beers
Thanks
you saved me a few hours even that i get payed by the hour :)
Thanks a lot. This was the only information that really help me.
Really thank you
Thank you!!!
This helped me!
Thanks a lot! It works now!
So, let's sum all the magic:
import MySQLdb
db=MySQLdb.connect(user="guest",passwd="guest",db="dbname",use_unicode=True)
db.set_character_set('utf8')
c=db.cursor()
c.execute('SET NAMES utf8;')
c.execute('SET CHARACTER SET utf8;')
c.execute('SET character_set_connection=utf8;')
Perfect! this recipe saves my life... Thanks.
Thank you!!!
This helped me!
Excellent, thanks for this!
Thank you! It helped me a lot!
I am still getting an error:
File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 146, in execute
query = query.encode(charset)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 134: ordinal not in range(128)
Am I doing something else wrong? Sorry I am new to python.
Thanks
I updated to version 1.2.3 of MySQLdb and it worked! Thanks
Thank you very much for the not waisted time!
Thanks! I've also wasted a lot of time trying to fix the database throught mysql when the problem was my python script.
Great job mate!
You saved me, dude! Live long and prosper :)
Yay, thanks man!
Thank you so so so so so so so much!! I have tried for long time but I'm finally seeing the light, thanks!
Thank you so so so so so so so so so so so so so much, I love you!
Da die Kontakte auf .de enden:
Vielen,vielen Dank.
Es geht mir wie all den anderen hier. Ich habe jahrelang mit einer
Krücke gelebt. Endlich kann ich meine quellen bereinigen
Thanks, alot
A lot of useless methods, mate. It is enough to connect with charset="utf8" param.
db = MySQLdb.connect(host="localhost", user = "root", passwd = "", db = "testdb", use_unicode=True, charset="utf8")
I'm not completly sure, but I think that the charset option was not available in 2007. But you are right, now this is surely the better solution.
Thankyou! Very useful article, it did actually save me heaps of time!
Thank you very much, this saved me a lot of time!