Home > Backend Development > Python Tutorial > Why Does My SQLite3 Code Throw an \'Incorrect Bindings Supplied\' Error?

Why Does My SQLite3 Code Throw an \'Incorrect Bindings Supplied\' Error?

Barbara Streisand
Release: 2024-12-05 01:15:10
Original
1028 people have browsed it

Why Does My SQLite3 Code Throw an

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

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

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

To resolve the error, a comma is necessary to create a tuple:

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

Alternatively, a list literal can be used:

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

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!

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