I'm trying to check whether a row exists and contains some expected values, but I keep getting this error:
Incorrect number of bindings supplied. The current statement uses 1, and there are 5 supplied.
Here's a minimal reproduction of the error:
import sqlite3
db_file = "test.db"
con = sqlite3.connect(db_file)
cur = con.cursor()
cur.execute('DROP TABLE IF EXISTS image')
cur.execute('''
CREATE TABLE image(
id INTEGER PRIMARY KEY,
listing_id TEXT NOT NULL,
url TEXT NOT NULL,
image BLOB NOT NULL
)''')
res = con.execute('''
SELECT listing_id, url FROM image WHERE listing_id=?
''', 'id123')
Result:
---------------------------------------------------------------------------
ProgrammingError Traceback (most recent call last)
Cell In[1], line 15
7 cur.execute('DROP TABLE IF EXISTS image')
8 cur.execute('''
9 CREATE TABLE image(
10 id INTEGER PRIMARY KEY,
(...)
13 image BLOB NOT NULL
14 )''')
---> 15 res = con.execute('''
16 SELECT listing_id, url FROM image WHERE listing_id=?
17 ''', 'id123')
ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 5 supplied.
What am I doing wrong here?
con.executeexpects an iterable, which a string is. So it's unpacking the characters letter-by-letter. Usecon.execute('''SELECT listing_id, url FROM image WHERE listing_id=?''', ('id123',))to make a tuple of the parameter and stop it unpacking letters