Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute SELECT * LIKE statement with a placeholder in sqlite?

Tags:

python

sql

sqlite

I've got an argument tag and I perfomed this way:

cursor.execute("SELECT * FROM posts WHERE tags LIKE '%?%'", (tag,))

but it doesn't seem to work.
I'm new to sqlite, please tell me how to fix it. Thx !

like image 520
yakiang Avatar asked Jan 03 '14 12:01

yakiang


1 Answers

Apply the wildcards to the parameter, not the SQL:

cursor.execute("SELECT * FROM posts WHERE tags LIKE ?", (f'%{tag}%',))

The ? SQL parameter interpolation adds quoting for you, so your query ends up as '%'value'%', which is not valid SQL.

like image 86
Martijn Pieters Avatar answered Nov 02 '22 09:11

Martijn Pieters