文章来源:http://www.dasprids.de/blog/2007/12/17/python-mysqldb-and-utf-8
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.
阅读(1424) | 评论(0) | 转发(0) |