【问题标题】:TypeError: expected a character buffer object while translatingTypeError:翻译时需要一个字符缓冲区对象
【发布时间】:2013-12-23 02:04:17
【问题描述】:

我正在开发一个使用 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) 将其转换为字符串,但问题就从这里开始了。它给了我这个:

[u'TEST'] <--- What is returned by the conversion to a string
None      <--- What is returned by the search function

当我将字典转换为字符串时。然后搜索函数返回None,因为它被传递给[u'TEST'],而不是像应该传递的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

我看了这些问题:

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

有谁知道是什么导致了类型错误?

谢谢!

【问题讨论】:

    标签: python sqlite python-2.7 dictionary


    【解决方案1】:

    我解决了这个问题。我在 Python 3.3.2+ 中运行了代码,它可以正常工作(尽管对翻译表进行了一些小调整)

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

    【讨论】:

    • @culix 完全不知道。这个项目是很久以前的事了,所以我不太记得了。对不起:(
    猜你喜欢
    • 1970-01-01
    • 2013-06-29
    • 2012-09-17
    • 2015-03-05
    • 1970-01-01
    • 2019-06-22
    • 2021-10-15
    相关资源
    最近更新 更多