python-3.xsqliteselectsql-likeinstr

does something like followin exists? ('''select * from TABLE where ? in FIELD''',(VAR))


does something like followin exist?

('''select * from TABLE where ? in FIELD''',(VAR))

I need to select all table rows where FIELD just contains the text of VAR (so not needed FIELD=VAR) I'm using python3 and sqlite3.


Solution

  • You can use the function INSTR():

    sql = "SELECT * FROM tablename WHERE INSTR(FIELD, ?)"
    

    or the operator LIKE:

    sql = "SELECT * FROM tablename WHERE FIELD LIKE '%' || ? || '%'"
    

    to get the rows that contain the value of VAR:

    execute(sql, (VAR,))