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.set_character_set('utf8')
dbc.execute('SET NAMES utf8;')
dbc.execute('SET CHARACTER SET utf8;')
dbc.execute('SET character_set_connection=utf8;')
"db" is the result of MySQLdb.connect, and "dbc" is the result of db.cursor().

I hope this will help some guys out, not waisting as much time as I did for this issue.

Comments to this article

  • Avatar of Rafael Barbolo Lopes Reply Rafael Barbolo Lopes Monday, January 21, 2008 1:24 AM

    I've also wasted a long time trying to fix this!

    Thanks

  • Avatar of Alex Reply Alex Sunday, February 24, 2008 6:23 PM

    thank you for sharing!

  • Avatar of THLopes Reply THLopes Monday, July 7, 2008 4:23 PM

    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,

  • Avatar of Miriam Ruiz Reply Miriam Ruiz Monday, April 6, 2009 5:12 PM

    Thanks! Your recipe has worked perfectly for me :)

  • Avatar of Alex Lopez Reply Alex Lopez Friday, June 19, 2009 6:45 PM

    You're the fucking master!!!!

    Our project owes you a few beers

  • Avatar of wearetherock Reply wearetherock Monday, August 24, 2009 6:26 AM

    Thanks

  • Avatar of Uriel Katz Reply Uriel Katz Wednesday, December 16, 2009 4:14 PM

    you saved me a few hours even that i get payed by the hour :)

  • Avatar of Daniel Pérez R. Reply Daniel Pérez R. Tuesday, December 22, 2009 3:55 PM

    Thanks a lot. This was the only information that really help me.

  • Avatar of Thierry Graff Reply Thierry Graff Friday, January 29, 2010 9:28 AM

    Really thank you

  • Avatar of Bojan Jovanovic Reply Bojan Jovanovic Monday, May 24, 2010 11:44 AM

    Thank you!!!

    This helped me!

  • Avatar of mclaudt Reply mclaudt Thursday, May 27, 2010 9:41 PM

    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;')

  • Avatar of warachet Reply warachet Tuesday, July 6, 2010 1:46 PM

    Perfect! this recipe saves my life... Thanks.

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.

 _    _                       
| | _(_) __ _  ___  ___  __ _ 
| |/ / |/ _` |/ _ \/ __|/ _` |
|   <| | (_| | (_) \__ \ (_| |
|_|\_\_|\__, |\___/|___/\__,_|
           |_|