简体   繁体   中英

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'

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%" })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM