【发布时间】:2011-02-19 18:05:18
【问题描述】:
我目前出于自己的目的将文件名保存在 sqlite 数据库中。每当我尝试插入具有特殊字符(如 é 等)的文件时,都会引发以下错误:
pysqlite2.dbapi2.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.
当我通过使用诸如unicode(filename) 之类的 unicode 方法包装发送到 pysqlite 的值来“将我的应用程序切换到 Unicode 字符串”时,它会引发此错误:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 66: ordinal not in range(128)
我可以做些什么来摆脱这种情况吗?修改我的所有文件以符合要求不是一种选择。
更新
如果我通过filename.decode("utf-8") 解码文本,我仍然会收到上面的 ProgrammingError。
我的实际代码如下所示:
cursor.execute("select * from musiclibrary where absolutepath = ?;",
[filename.decode("utf-8")])
我的代码应该是什么样的?
【问题讨论】:
-
看起来这段代码,在你更新问题后,实际上不是产生错误的代码,对吧?
-
对,后来在应用程序中是类似的代码。