pysqlite2:编程错误 - 不得使用 8 位字节串

发布于 2024-09-01 06:43:25 字数 899 浏览 5 评论 0原文

我目前出于自己的目的将文件名保存在 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 字符串”时,如下所示: unicode(filename),它会抛出此错误:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 66: ordinal not in range(128)

我可以做些什么来摆脱这个错误吗?修改我的所有文件以使其一致并不是一种选择。

更新 如果我通过 filename.decode("utf-8") 解码文本,我仍然会收到上面的编程错误。

我的实际代码如下所示:

cursor.execute("select * from musiclibrary where absolutepath = ?;",
    [filename.decode("utf-8")])

我的代码应该是什么样子?

I'm currently persisting filenames in a sqlite database for my own purposes. Whenever I try to insert a file that has a special character (like é etc.), it throws the following error:

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.

When I do "switch my application over to Unicode strings" by wrapping the value sent to pysqlite with the unicode method like: unicode(filename), it throws this error:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 66: ordinal not in range(128)

Is there something I can do to get rid of this? Modifying all of my files to conform isn't an option.

UPDATE
If I decode the text via filename.decode("utf-8"), I'm still getting the ProgrammingError above.

My actual code looks like this:

cursor.execute("select * from musiclibrary where absolutepath = ?;",
    [filename.decode("utf-8")])

What should my code here look like?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

旧情别恋 2024-09-08 06:43:26

您是否尝试过直接传递 unicode 字符串:

cursor.execute("select * from musiclibrary where absolutepath = ?;",(u'namé',))

您需要在脚本开头添加文件编码:

# coding: utf-8

Have you tried to pass the unicode string directly:

cursor.execute("select * from musiclibrary where absolutepath = ?;",(u'namé',))

You will need to add the file encoding at the beginning of the script:

# coding: utf-8
梅倚清风 2024-09-08 06:43:26

您已经弄清楚了这一点,但是:

我认为您实际上无法从cursor.execute("select * from musiclibrary where Absolutepath = ?;", [filename.decode("utf-8")]) 获得ProgrammingError 异常,正如问题目前所述。

要么 utf-8 解码会崩溃,要么cursor.execute 调用会对结果感到满意。

You figured this out already, but:

I don't think you could actually get that ProgrammingError exception from cursor.execute("select * from musiclibrary where absolutepath = ?;", [filename.decode("utf-8")]), as the question currently states.

Either the utf-8 decode would explode, or the cursor.execute call would be happy with the result.

薯片软お妹 2024-09-08 06:43:26

尝试更改为:

cursor.execute("select * from musiclibrary where absolutepath = ?;",
    [unicode(filename,'utf8')])

在您的文件名来源中不使用 utf8 进行编码,将 utf8 更改为您的编码。

Try to change to this:

cursor.execute("select * from musiclibrary where absolutepath = ?;",
    [unicode(filename,'utf8')])

In your filename origin not encode with utf8, change utf8 to your encoding.

愿得七秒忆 2024-09-08 06:43:25

您需要指定 filename 的编码以转换为 Unicode,例如:filename.decode('utf-8')。仅使用 unicode(...) 选择控制台编码,这通常是不可靠的(并且通常是 ascii)。

You need to specify the encoding of filename for conversion to Unicode, for example: filename.decode('utf-8'). Just using unicode(...) picks the console encoding, which is often unreliable (and often ascii).

挖个坑埋了你 2024-09-08 06:43:25

您应该将 SQL 语句的参数作为 Unicode 传递。

现在,这完全取决于您如何获取文件名列表。也许您正在使用 os.listdir 或 os.walk 读取文件系统?如果是这种情况,有一种方法可以直接将文件名设置为 Unicode,只需将 Unicode 参数传递给以下函数之一即可:
示例:

  • os.listdir(u'.')
  • os.walk(u'.')

当然,您可以替换 u'.' code> 目录,其中包含您正在阅读其内容的实际目录。只要确保它是一个 Unicode 字符串即可。

You should pass as Unicode the arguments of your SQL statement.

Now, it all depends on how you obtain the filename list. Perhaps you're reading the filesystem using os.listdir or os.walk? If that is the case, there is a way to have directly the filenames as Unicode just by passing a Unicode argument to either of these functions:
Examples:

  • os.listdir(u'.')
  • os.walk(u'.')

Of course, you can substitute the u'.' directory with the actual directory whose contents you are reading. Just make sure it's a Unicode string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文