Home > Backend Development > Python Tutorial > Why Am I Getting an \'Incorrect Number of Bindings Supplied\' Error in My SQLite3 Code?

Why Am I Getting an \'Incorrect Number of Bindings Supplied\' Error in My SQLite3 Code?

Barbara Streisand
Release: 2024-11-27 08:49:17
Original
658 people have browsed it

Why Am I Getting an

Binding Error in SQLite3

When attempting to execute a SQL query with multiple bindings using sqlite3, you may encounter the "Incorrect number of bindings supplied" error message. This error occurs when the number of provided bindings does not match the number of parameters defined in the query.

In the example code:

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()
Copy after login

When attempting to insert a string 74 characters long, you receive the error message, indicating a mismatch between the number of bindings and parameters.

To fix this issue, ensure that you pass in a valid sequence as the parameter. In this case, the parentheses around the img variable should include a comma to create a tuple:

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

Alternatively, you can use a list literal:

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

By correcting the parameter, you will provide the correct number of bindings for the query, resolving the error and allowing you to insert the data successfully.

The above is the detailed content of Why Am I Getting an \'Incorrect Number of Bindings Supplied\' Error in My SQLite3 Code?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template