SQLite3 Error: Incorrect Bindings Supplied
When executing SQL queries with bind parameters, it's crucial to ensure the correct number of bindings is supplied. In the given code, the following error is encountered:
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 74 supplied
Upon examination of the 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()
It becomes clear that the issue lies in the way the bind parameter is specified in the cursor.execute statement:
cursor.execute('INSERT INTO images VALUES(?)', (img))
To resolve the error, a comma is necessary to create a tuple:
cursor.execute('INSERT INTO images VALUES(?)', (img,))
Alternatively, a list literal can be used:
cursor.execute('INSERT INTO images VALUES(?)', [img])
By ensuring the bind parameter is correctly supplied as a tuple or list, the correct number of bindings will be used, resolving the error.
The above is the detailed content of Why Does My SQLite3 Code Throw an \'Incorrect Bindings Supplied\' Error?. For more information, please follow other related articles on the PHP Chinese website!