Python, MySQLdb and UTF-8
Monday, December 17, 2007So, 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().
dbc.execute('SET NAMES utf8;')
dbc.execute('SET CHARACTER SET 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.