sqlite3 error: Incorrect number of bindings supplied

avatar
Borislav Hadzhiev

Last updated: Apr 13, 2024
3 min

banner

# sqlite3 error: Incorrect number of bindings supplied

The Python error "sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are X supplied" occurs when you pass a string or other literal instead of a tuple to the cursor.execute() method.

You can parenthesize the parameter and add a comma to make it into a tuple to solve the error.

Here is an example of how the error occurs.

main.py
import sqlite3 connection = sqlite3.connect('movies.db') cursor = connection.cursor() # โ›”๏ธ sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 7 supplied. cursor.execute('INSERT into movies VALUES(?)', ('movie A'))

sqlite incorrect number of bindings supplied

The issue in the code sample is that we are passing a string as the second argument to cursor.execute() when a sequence is expected.

Wrapping a single string in parentheses does not make it a tuple.

You can use the type class to verify the value's type.

main.py
a_str = ('movie A') print(type(a_str)) # ๐Ÿ‘‰๏ธ <class 'str'> print(isinstance(a_str, str)) # ๐Ÿ‘‰๏ธ True

mistakenly declared string instead of tuple

# Place a comma after the string to make it a tuple

You can place a comma after the string to make it a tuple to solve the error.

main.py
import sqlite3 connection = sqlite3.connect('movies.db') cursor = connection.cursor() cursor.execute('INSERT into movies VALUES(?)', ('movie A', ))

place comma after the string to solve the error

I updated the second parameter from:

main.py
# ๐Ÿ‘‡๏ธ This is a string ('movie A')

To:

main.py
# ๐Ÿ‘‡๏ธ This is a tuple ('movie A', )

You can use the type() class to verify that the value is a tuple.

main.py
a_tuple = ('movie A', ) print(type(a_tuple)) # ๐Ÿ‘‰๏ธ <class 'tuple'> print(isinstance(a_tuple, tuple)) # ๐Ÿ‘‰๏ธ True

verify that the value is a tuple

The error message stated "sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 7 supplied." because 1 parameter is expected and 7 parameters are supplied.

This is because the string "movie A" has exactly 7 characters.

main.py
print(len('movie A')) # ๐Ÿ‘‰๏ธ 7

You can imagine that the string was split on each character and passed as multiple arguments to the cursor.execute() method.

# Using a list to solve the error

You can also use a list to solve the error, you don't necessarily have to use a tuple.

main.py
import sqlite3 connection = sqlite3.connect('movies.db') cursor = connection.cursor() cursor.execute('INSERT into movies VALUES(?)', ['movie A'])

using a list to solve the error

You just have to make sure that the second argument you pass to cursor.execute() is a sequence (not a string).

# Make sure the number of columns is the same as the number of supplied values

Another common cause of the error is when there's a mismatch between the number of columns and the number of supplied values.

main.py
import sqlite3 connection = sqlite3.connect('movies.db') cursor = connection.cursor() # โ›”๏ธ sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 2 supplied. cursor.execute( 'INSERT into movies VALUES(?)', ['movie A', 'movie B'] )

mismatch between number of columns and number of supplied values

Notice that we only have 1 column VALUES(?) in the example, but we've supplied 2 values as the second parameter to cursor.execute().

You have to make sure the number of columns matches the number of supplied values to insert.

main.py
import sqlite3 connection = sqlite3.connect('movies.db') cursor = connection.cursor() cursor.execute( 'INSERT into movies VALUES(?, ?)', ['movie A', 'movie B'] )

The code sample above assumes that the table has 2 columns, so we have 2 question marks and 2 values in the list.

I've also written an article on how to open, view and query SQLite in VS Code.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev