Last updated: Apr 13, 2024
Reading timeยท3 min
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.
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'))
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.
a_str = ('movie A') print(type(a_str)) # ๐๏ธ <class 'str'> print(isinstance(a_str, str)) # ๐๏ธ True
You can place a comma after the string to make it a tuple to solve the error.
import sqlite3 connection = sqlite3.connect('movies.db') cursor = connection.cursor() cursor.execute('INSERT into movies VALUES(?)', ('movie A', ))
I updated the second parameter from:
# ๐๏ธ This is a string ('movie A')
To:
# ๐๏ธ This is a tuple ('movie A', )
You can use the type()
class to verify that the value is a tuple.
a_tuple = ('movie A', ) print(type(a_tuple)) # ๐๏ธ <class 'tuple'> print(isinstance(a_tuple, tuple)) # ๐๏ธ True
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.
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.
You can also use a list to solve the error, you don't necessarily have to use a tuple.
import sqlite3 connection = sqlite3.connect('movies.db') cursor = connection.cursor() cursor.execute('INSERT into movies VALUES(?)', ['movie A'])
You just have to make sure that the second argument you pass to
cursor.execute()
is a sequence (not a string).
Another common cause of the error is when there's a mismatch between the number of columns and the number of supplied values.
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'] )
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.
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.
You can learn more about the related topics by checking out the following tutorials: