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.