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()
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,))
Alternatively, you can use a list literal:
cursor.execute('INSERT INTO images VALUES(?)', [img])
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!