Python sqlite3.ProgrammingError:提供的绑定数量不正确。当前语句使用 1,并且提供了 74

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16856647/
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-18 23:49:30  来源:igfitidea点击:

sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 74 supplied

pythonsqlite

提问by AB49K

def insert(array):
    connection=sqlite3.connect('images.db')
    cursor=connection.cursor()
    cnt=0
    while cnt != len(array):
            img = array[cnt]
            print(array[cnt])
            cursor.execute('INSERT INTO images VALUES(?)', (img))
            cnt+= 1
    connection.commit()
    connection.close()

I cannot figure out why this is giving me the error, The actual string I am trying to insert is 74 chars long, it's: "/gifs/epic-fail-photos-there-i-fixed-it-aww-man-the-tire-pressures-low.gif"

我不知道为什么这会给我错误,我尝试插入的实际字符串长 74 个字符,它是:“/gifs/epic-fail-photos-there-i-fixed-it-aww-man-the -轮胎压力-low.gif"

I've tried to str(array[cnt]) before inserting it, but the same issue is happening, the database only has one column, which is a TEXT value.

我在插入之前尝试过 str(array[cnt]) ,但发生了同样的问题,数据库只有一列,这是一个 TEXT 值。

I've been at it for hours and I cannot figure out what is going on.

我已经研究了几个小时,我无法弄清楚发生了什么。

采纳答案by Martijn Pieters

You need to pass in a sequence, but you forgot the comma to make your parameters a tuple:

您需要传入一个序列,但您忘记了逗号以使您的参数成为元组:

cursor.execute('INSERT INTO images VALUES(?)', (img,))

Without the comma, (img)is just a grouped expression, not a tuple, and thus the imgstring is treated as the input sequence. If that string is 74 characters long, then Python sees that as 74 separate bind values, each one character long.

没有逗号,(img)它只是一个分组表达式,而不是元组,因此img字符串被视为输入序列。如果该字符串的长度为 74 个字符,则 Python 会将其视为 74 个单独的绑定值,每个值一个字符长。

>>> len(img)
74
>>> len((img,))
1

If you find it easier to read, you can also use a list literal:

如果你觉得更容易阅读,你也可以使用列表文字:

cursor.execute('INSERT INTO images VALUES(?)', [img])

回答by Punnerud

cursor.execute(sql,array)

Only takes two arguments.
It will iterate the "array"-object and match ? in the sql-string.
(with sanity checks to avoid sql-injection)

只需要两个参数。
它将迭代“数组”对象并匹配 ? 在 sql 字符串中。
(通过健全性检查以避免 sql 注入)