Using a WHERE ___ IN ___ statement Using a WHERE ___ IN ___ statement sqlite sqlite

Using a WHERE ___ IN ___ statement


You need to create enough parameters to match your list of vars:

statement = "SELECT * FROM tab WHERE obj IN ({0})".format(', '.join(['?'] * len(list_of_vars)))c.execute(statement, list_of_vars)

Note that you pass in list_of_vars as the parameter values list. Using the ', '.join() we generate a string of ? characters separated by commas, then use .format() to insert that into the statement.

For a long list of variables, it may be more efficient to use a temporary table to hold those values, then use a JOIN against the temporary table rather than an IN clause with bind parameters.


FYI, pymysql with MySQL user.

query ="SELECT * FROM tab WHERE obj IN %s"cursor.execute(query, (['foo','bar'],))same ascursor.execute(query, (list_of_var,)) 

I'm not sure about sqlite3,this may work,

query ="SELECT * FROM tab WHERE obj IN ?"cursor.execute(query, (['foo','bar'],)) orcursor.execute(query, (list_of_vars,))