Sqlite3报错sqlite3.OperationalError: Could not decode to UTF-8 column XXX with text '***'

Python2.7+sqlite3执行execute(sql)时,报错:

Traceback (most recent call last):
  File "sqlitetest.py", line x, in
    c.execute('select ccc from tbl')
sqlite3.OperationalError: Could not decode to UTF-8 column 'ccc' with text '\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd'

这个意思是说column ‘ccc’ 不能通过UTF-8 decode,就是ccc不是用utf8编码的,这个可以通过设置 conn.text_factory 解决
如 conn.text_factory = str就不会出错了(如果是Python3可能是bytes,待验证),当然最好还是搞清楚字段的编码,或者入库时对字段内容进行编码。

你可能感兴趣的:(python)