【发布时间】: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 TypeError: expected a character buffer object, personal misunderstanding - 不适用于我的问题
expected buffer object error on string.translate - python 2.6 - 也不适用,因为我的翻译表是从字符串而不是字典创建的
Getting error "expected character buffer object" and I don't know why - 也不适用,因为他正试图在索引处执行此操作。我正在尝试做它应该做的事情,在整个字符串中查找/替换。
但没有一个适用于我的问题(据我所知。)
有谁知道是什么导致了类型错误?
谢谢!
【问题讨论】:
标签: python sqlite python-2.7 dictionary