TypeError:转换时应为字符缓冲区对象

2024-10-01 04:46:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个项目,它使用sqlite3数据库来存储一些数据。在

我的搜索函数使用SQL语句:'''SELECT text FROM snippets WHERE title=?''', (whichName,),正如我的代码所示,whichName作为一个字典出现,这导致了以下错误:

Traceback (most recent call last):
  File "snippets.py", line 93, in <module>
    main()
  File "snippets.py", line 24, in main
    get_value_from_name(response)
  File "snippets.py", line 58, in get_value_from_name
    (whichName,))
sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type.

因此,我认为我需要将它作为字符串传递,所以我只是name = str(response)将其转换为字符串,但问题就从这里开始了。它给了我这个:

^{pr2}$

当我把字典转换成字符串时。然后搜索函数返回None,因为它是按[u'TEST']而不是{}传递的。所以,我添加了一些翻译代码:

    translation_table = dict.fromkeys(map(ord, '(),'), None)
    # Above code creates a table with the mapped characters

    name = str(response)

    name = name.translate(translation_table)

这就是我目前的问题所在。它返回以下错误:

Traceback (most recent call last):
  File "snippets.py", line 93, in <module>
    main()
  File "snippets.py", line 20, in main
    name = name.translate(translation_table)
TypeError: expected a character buffer object

我看了这些问题:

但没有一个适用于我的问题(据我所知)

有人知道是什么导致了类型错误吗?在

谢谢!在


Tags: 字符串nameinpy字典objectmainbuffer
1条回答
网友
1楼 · 发布于 2024-10-01 04:46:38

我解决了这个问题。我在python3.3.2+中运行了代码,它的工作原理是find(尽管对翻译表做了一些小的调整)

对不起,如果我浪费了别人的时间。我会尽快将此标记为答案。在

相关问题 更多 >