ElisaFo
ElisaFo

Reputation: 140

How to find a substring in a Sqlite table?

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

Answers (1)

DinoCoderSaurus
DinoCoderSaurus

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

Related Questions