How to fix SQLite3 Python 'Incorrect number of bindings supplied. The current statement uses 1, ... supplied'
Problem:
You are trying to run a simple SQL query with placeholders on a SQLite3 database, e.g.:
sqlite_incorrect_bindings_fix.py
name = "mytable"
conn.execute("SELECT sql FROM sqlite_master WHERE name=?;", name)But you get an exception like this:
example.txt
---------------------------------------------------------------------------
ProgrammingError Traceback (most recent call last)
<ipython-input-55-e385cf40fd72> in <module>
1 name = "mytable"
----> 2 conn.execute("SELECT sql FROM sqlite_master WHERE type='table' AND name=?;", name)
ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 7 supplied.Solution
You need to use a list as the second argument to conn.execute(...)!
Since you only gave the function a string, the string is being interpreted as list of characters.
In our example from above, you just need to wrap name in square brackets to read [name]:
example.py
name = "mytable"
conn.execute("SELECT sql FROM sqlite_master WHERE name=?;", [name])If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow