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

Posted on

Solving problem is about exposing yourself to as many situations as possible like sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 74 supplied and practice these strategies over and over. With time, it becomes second nature and a natural way you approach any problems in general. Big or small, always start with a plan, use other strategies mentioned here till you are confident and ready to code the solution.
In this post, my aim is to share an overview the topic about sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 74 supplied, which can be followed any time. Take easy to follow this discuss.

sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 74 supplied
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”

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.

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

Asked By: AB49K

||

Answer #1:

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 img string 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.

>>> 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])
Answered By: Martijn Pieters

Answer #2:

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)

Answered By: Punnerud
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.