Reputation: 140
I am creating a database with python Sqlite3 and I am trying to collect some specific rows in this table. This is the format for each row:
{
"DATA":"my_variable Test should12 be substring of this",
}
Here is that part of my code which I need to solve:
rows = curs.execute("SELECT * FROM table WHERE DATA LIKE :my_variable", {"my_variable": my_variable}).fetchall()
print(rows)
And my_variable could take any string. my_variable is a python string and is my_variable= 'should12'
Upvotes: 1
Views: 170
Reputation: 6520
The sqlite LIKE operator is used for pattern matching. And you should use parametrized queries to prevent (the very real) threat of SQL injection.
The statement will be something like this (using named style)
curs.execute("SELECT * FROM table WHERE LOWER(DATA) LIKE :my_variable",{"my_variable" : "%should12 be%" })
Upvotes: 1