【发布时间】:2011-03-26 09:59:00
【问题描述】:
在 Python 中使用 SQLite3,我正在尝试存储 UTF-8 HTML 代码的 sn-p 的压缩版本。
代码如下:
...
c = connection.cursor()
c.execute('create table blah (cid integer primary key,html blob)')
...
c.execute('insert or ignore into blah values (?, ?)',(cid, zlib.compress(html)))
在什么时候得到错误:
sqlite3.ProgrammingError: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings.
如果我使用 'text' 而不是 'blob' 并且不压缩 HTML sn-p,它可以正常工作(虽然 db 很大)。当我使用“blob”并通过 Python zlib 库进行压缩时,我收到上述错误消息。我环顾四周,但找不到一个简单的答案。
【问题讨论】:
标签: python unicode sqlite zlib