我目前正在为自己的目的将文件名存储在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方法包装发送给pysqlite的值,例如unicode(filename)
来将我的应用程序切换到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")])
这里的代码应该长什么样?