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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21981709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:01:16  来源:igfitidea点击:

Error binding parameter 0: probably unsupported type

pythonpython-3.xsqlite

提问by user3345626

I can't seem to figure out what is wrong with my code, but I keep getting the:

我似乎无法弄清楚我的代码有什么问题,但我不断收到:

error "binding parameter 0 - probably unsupported type". 

Here is my code:

这是我的代码:

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)

When I have it where 'EBERT' is in the query and not set to a variable, it works fine, so I know it's a problem with the tuple syntax or something. I've tried it without the brackets, with a second variable for first_name, with and without a separately defined cursor, and basically every method I can think of, and I've researched for hours but have gotten nowhere, so any help would be super appreciated.

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

采纳答案by falsetru

Nested lists, tuples are used for executemany, not for execute.

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

Pass a flat list (or tuple) that contians parameters.

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

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

or

或者

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

回答by Anum Sheraz

I was getting the same error, sorted out that my data type was mismatched. I then converted it into string;

我遇到了同样的错误,发现我的数据类型不匹配。然后我把它转换成字符串;

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

and it worked fine. Hope this will be helpful for someone.

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

回答by Ronald Saunfe

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

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

this worked for me

这对我有用