0

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?

1
  • 1
    The second argument to con.execute expects an iterable, which a string is. So it's unpacking the characters letter-by-letter. Use con.execute('''SELECT listing_id, url FROM image WHERE listing_id=?''', ('id123',)) to make a tuple of the parameter and stop it unpacking letters Commented Feb 20, 2024 at 9:49

1 Answer 1

1

The error you're getting is because you're passing a string 'id123' when the execute() method is expecting a tuple for its parameters. When you provide a string directly, it treats each character in the string as a separate parameter, leading to the incorrect number of bindings error.

Here's how you can fix the error:

res = con.execute('''
    SELECT listing_id, url FROM image WHERE listing_id=?
    ''', ('id123',))
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.