我正在尝试使用Twisted adbapi将二进制数据存储在sqlite数据库中。但是,当我运行查询来存储数据时,我收到一个错误:
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.
在谷歌搜索后,我找到了正常sqlite连接的答案:
con = sqlite3.connect(...)
con.text_factory = str
但是,我找不到与扭曲的adbapi sqlite连接一起使用的等效设置:
dbpool = adbapi.ConnectionPool("sqlite3", "data.db", check_same_thread=False)
我将不胜感激任何帮助!
答案 0 :(得分:2)
我明白了。要在打开后更改连接,必须使用ConnectionPool的cp_openfun
参数。以下代码有效:
def set_text_factory(conn):
conn.text_factory = str
dbpool = adbapi.ConnectionPool("sqlite3", "data.db", check_same_thread=False,
cp_openfun=set_text_factory)