Mr. Hax
Mr. Hax

Reputation: 305

How do you SELECT all rows of an sqlite3 database that have a column that contains a certain variable substring?

I know how to use the LIKE clause to extract rows that have a column that contains a hard-coded substring:

SELECT * FROM userTable WHERE UPPER(column) LIKE UPPER('%this_is_a_hard_coded_string%')

But I can't figure out how to use a variable for the substring instead of a hardcoded substring. Usually, when I make an sqlite3 query with python3, I can set a value equal to a ? and then add an extra argument to the execute method on the cursor; however, that does not seem to work here.

Upvotes: 2

Views: 161

Answers (1)

srp
srp

Reputation: 585

Try this

t = ("%"+"this_is_a_hard_coded_string"+"%",)
c.execute('SELECT * FROM userTable WHERE UPPER(column) LIKE UPPER(?)', t)

You can also use a different more readable use of "?".

Refer this answer

https://stackoverflow.com/a/1010804/1471352

or this

https://stackoverflow.com/a/3105370/1471352

Upvotes: 1

Related Questions