Animeartist
Animeartist

Reputation: 1187

How to give parameters in SQL query having LIKE keyword

I have a Workers SQLite table and I am trying to display all workers whose name starts with a particular character input by the user.

letter = input("\nEnter starting letter of worker: ")
(cursor.execute("SELECT * FROM Workers WHERE Worker_Name LIKE '(?)%'", (letter,))).fetchall()

But I always keep getting this error:

sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 0, and there are 1 supplied.

But when I give like this, it works:

(cursor.execute("SELECT * FROM Workers WHERE Worker_Name LIKE 'J%'")).fetchall()

Upvotes: 0

Views: 95

Answers (1)

GMB
GMB

Reputation: 222722

Use string concatenation in the query:

cursor.execute("SELECT * FROM Workers WHERE Worker_Name LIKE ? || '%'", (letter,))

... Or use string concatenation in your application code:

cursor.execute("SELECT * FROM Workers WHERE Worker_Name LIKE ?", (letter + "%",))

Upvotes: 2

Related Questions