加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

sqlite3.OperationalError: Could not decode to UTF-8 column X

发布时间:2020-12-12 20:24:01 所属栏目:百科 来源:网络整理
导读:环境:python3.2 sqllite3 代码如下: import sqlite3 as sql conn = sql.connect(r'c:setupinfidb.db',detect_types=sql.PARSE_COLNAMES) c = conn.cursor() c.execute('select * from setuplog') for row in c: print(row) 运行以上代码时,提示: Traceb

环境:python3.2 sqllite3
代码如下:
import sqlite3 as sql

conn = sql.connect(r'c:setupinfidb.db',detect_types=sql.PARSE_COLNAMES)
c = conn.cursor()
c.execute('select * from setuplog')
for row in c:
print(row)

运行以上代码时,提示:
Traceback (most recent call last):
File "sqlitetest.py",line x,in <module>
c.execute('select * from setuplog')
sqlite3.OperationalError: Could not decode to UTF-8 column 'logtype' with text 'ufffdufffdufffdufffdufffdufffdufffdufffdufffdufffd'

这个意思是说column ‘logtype’ 不能通过UTF-8 decode,就是logtype不是用utf8编码的 一般情况下这个情况出现在text类型的数据上面 这个可以通过设置 conn.text_factory 解决 如 conn.text_factory = bytes 把text类型当bytes来解释,就不会出错了 不过,这样也不太好,如果知道是什么编码就好了,例子代码是gbk编码的 这里可以这样设置: conn.text_factory = lambda x : str(x,'gbk','ignore') 指示以gbk来解码而不是默认的utf8

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读