异常现象:
使用Python通过MySQLdb向Mysql数据库写入中文的时候的异常处理,插入的字段含有中文,比如:
比如插入数据:
sql = ‘insert into student(id,name,age)’
’ values(’{0}’,’{1}’,’{2}’)’.format(21,“王莽”,36)
会出现如下异常:
‘charmap’ codec can’t encode characters in position 46-47: character maps to
解决办法:
数据库的连接语句:
conn = MySQLdb.connect(
user = ‘root’,
passwd = ‘123456’,
host = ‘localhost’,
port = 3306,
db = ‘lvtest’
)
修改为:
conn = MySQLdb.connect(
user = ‘root’,
passwd = ‘123456’,
host = ‘localhost’,
port = 3306,
db = ‘lvtest’,
charset = 'utf8’
)