您的位置:首页 > 编程语言 > C语言/C++

Mysql数据库操作字符集编码乱码错误 UnicodeEncodeError 'latin-1' - C/C++转Python疑难杂症(一)

2017-01-08 00:00 351 查看
在使用数据库时

# 错误内容
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 86-91: ordinal not in range(256)


头部加入

#coding=utf-8


设置默认字符集

import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )


设置数据库字符集

db.set_character_set('utf8')
dbcour.execute('SET NAMES utf8;')
dbcour.execute('SET CHARACTER SET utf8;')
dbcour.execute('SET character_set_connection=utf8;')


进入数据库查询数据库表及列的字符集是否属于utf8

我使用已经写好的 INSERT 插入语句 结果正常

使用 _info 就会提示 UnicodeEncodeError: 'latin-1' codec can't encode characters in position 87-92: ordinal not in range(256)

最后我利用修改数据库字符集的方式解决了这个问题

我遇到这个问题的代码如下

#coding=utf-8

import os
import MySQLdb
import sys reload(sys) sys.setdefaultencoding( "utf-8" )

# 插入数据库
# 参数 _info : 列表 [部门、姓名、工号、时间、签到机型、比对方式]
# 例如 ["星际控股集团", "徐翼", 11, "2016/12/1 08:29:29", 1, "指纹"]
def insertAttendTable(_info):
# 连接到数据库
db = MySQLdb.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='test')
dbcour = db.cursor()
############################################## 下方未注释部分 方法1
insert_attend_sql = "INSERT INTO `attendance_all` (`department`, `name`,`id`,`attend_time`, `mac`) VALUES ('%s', '%s', '%s', '%s', '%s');" \
% (_info["department"], _info["name"], _info["id"], _info["time"], _info["mac"])
# insert_attend_sql = "INSERT INTO `test`.`attendance_all` (`name`, `id`, `department`, `mac`, `attend_time`, `create_time`, `mark`) VALUES ('徐翼', '11', '星际控股集团', '1', '2016-12-01 08:29:29', '2017-01-07 23:44:00', NULL);"
################################ 上方注释部分 方法2
dbcour.execute(insert_attend_sql)
db.commit()
return

# 分析行数据
def analysisLine(_lineInfo):
# print _lineInfo
attend_department = u"星际控股集团"
attend_name = u"徐翼"
attend_id = 11
attend_time = u"2016/12/1 08:29:29"
attend_mac = 1
attend_analysis = u"指纹"

info = {"department":attend_department,
"name":attend_name,
"id":attend_id,
"time":attend_time,
"mac":attend_mac,
"analysis":attend_analysis}

return info

# 分析文件
def readFile(_fileName):
print "begin"

file = open(_fileName)
# 判断打开
if not file:
print "error file open failed!"
return

while 1:
lines = file.readlines(100000)
if not lines:
break

for line in lines:
info = analysisLine(line)
insertAttendTable(info)

file.close()
print "end"

readFile("1212.txt")


1212.txt

企管部   黄金成    14715 2016/12/26 12:44:33      1          指纹
企管部   黄金成    14715 2016/12/26 13:23:26      1          指纹
企管部   黄金成    14715 2016/12/26 17:39:38      1          指纹
企管部   黄金成    14715 2016/12/27 08:17:58      1          指纹
企管部   黄金成    14715 2016/12/27 13:05:01      1          指纹
企管部   黄金成    14715 2016/12/27 13:05:05      1          指纹
企管部   黄金成    14715 2016/12/27 18:47:25      1          指纹
企管部   黄金成    14715 2016/12/28 08:22:55      1          指纹
企管部   黄金成    14715 2016/12/28 13:06:48      1          指纹
企管部   黄金成    14715 2016/12/28 13:06:51      1          指纹
企管部   黄金成    14715 2016/12/28 18:08:37      1          指纹
企管部   黄金成    14715 2016/12/29 08:24:14      1          指纹
企管部   黄金成    14715 2016/12/29 13:12:33      1          指纹
企管部   黄金成    14715 2016/12/29 13:12:37      1          指纹
企管部   黄金成    14715 2016/12/29 17:39:05      1          指纹
企管部   黄金成    14715 2016/12/30 08:24:40      1          指纹
企管部   黄金成    14715 2016/12/30 12:30:40      1          指纹
企管部   黄金成    14715 2016/12/30 12:30:45      1          指纹
企管部   黄金成    14715 2016/12/30 17:33:53      1          指纹


总结

整体问题,非常明了,就是字符集问题。

从这个问题出发,我们应该能够检查的无非这几个地方

本地编码、数据库编码

在C++中,我们在初始化数据库时就会使用charset来设置调用接口的处理字符集。

在python中也是如此。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐