错误绑定参数0:可能不支持的类型

时间:2014-02-24 07:43:27

标签: python python-3.x sqlite

我似乎无法弄清楚我的代码有什么问题,但我一直得到:

error "binding parameter 0 - probably unsupported type". 

这是我的代码:

last = 'EBERT'

sakila = connect("sakila.db")
res = sakila.execute("SELECT first_name, last_name FROM customer WHERE last_name = ?",[(last,)])

for row in res:
    print(row)

当我在查询中“EBERT”并且未设置为变量时,它工作正常,所以我知道这是元组语法或其他问题。我已经尝试过没有括号,第first_name个变量,有和没有单独定义的光标,基本上我能想到的每一种方法,我已经研究了几个小时但是没有任何地方,所以任何帮助都会受到超级赞赏。

3 个答案:

答案 0 :(得分:4)

嵌套列表,元组用于executemany,而不是execute

传递一个包含参数的平面列表(或元组)。

res = sakila.execute(
    "SELECT first_name, last_name FROM customer WHERE last_name = ?",
    (last,))

res = sakila.execute(
    "SELECT first_name, last_name FROM customer WHERE last_name = ?",
    [last])

答案 1 :(得分:4)

我收到同样的错误,整理出我的数据类型不匹配。然后我把它转换成字符串;

cursor.execute('''INSERT INTO employees VALUES (?);''', (str(data[0]), ))

它工作正常。希望这对某人有所帮助。

答案 2 :(得分:0)

res = sakila.execute(     ''' SELECT first_name,last_name FROM customer WHERE last_name = {0}''' .format(last))

这对我有用