Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4583916
  • 博文数量: 1214
  • 博客积分: 13195
  • 博客等级: 上将
  • 技术积分: 9105
  • 用 户 组: 普通用户
  • 注册时间: 2007-01-19 14:41
个人简介

C++,python,热爱算法和机器学习

文章分类

全部博文(1214)

文章存档

2021年(13)

2020年(49)

2019年(14)

2018年(27)

2017年(69)

2016年(100)

2015年(106)

2014年(240)

2013年(5)

2012年(193)

2011年(155)

2010年(93)

2009年(62)

2008年(51)

2007年(37)

分类: Mysql/postgreSQL

2012-08-27 17:49:37

文章来源: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.
阅读(1432) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~