I'm trying to safely build an SQL query from a given string. The string can contain whitespaces.
string = "chair"
query = """SELECT *
FROM albums
WHERE title LIKE %s"""
values = ('%' + string + '%',)
cur.execute(query,values)
# >> SELECT * FROM albums WHERE title LIKE '%chair%'
But I can't figure out how to safely extend this to search for multiple words with the AND operator.
string = "big blue chair"
# >> SELECT * FROM albums WHERE title LIKE '%big%' AND
# title LIKE '%blue%' AND
# title LIKE '%chair%'
(I know this is what FULLTEXT searches are for, etc ... but this is for a very small dataset and is fast enough.)
values = ["%{}%".format(val) for val in string.split()]? Not being snarky, I'm just not sure what your concern is.