SQLite: bind list of values to "WHERE col IN ( :PRM )" SQLite: bind list of values to "WHERE col IN ( :PRM )" sqlite sqlite

SQLite: bind list of values to "WHERE col IN ( :PRM )"


You can dynamically build a parameterized SQL statement of the form

 SELECT * FROM TABLE WHERE col IN (?, ?, ?)

and then call sqlite_bind_int once for each "?" you added to the statement.

There is no way to directly bind a text parameter to multiple integer (or, for that matter, multiple text) parameters.

Here's pseudo code for what I have in mind:

-- Args is an array of parameter valuesfor i = Lo(Args) to Hi(Args)   paramlist = paramlist + ', ?'sql = 'SELECT * FROM TABLE WHERE col IN (' + Right(paramlist, 3)  + ')'for i = Lo(Args) to Hi(Args)  sql_bind_int(sql, i, Args[i]-- execute query here.


I just faced this question myself, but answered it by creating a temporary table and inserting all the values into that, so that I could then do:

SELECT * FROM TABLE WHERE col IN (SELECT col FROM temporarytable);


Even simpler, build your query like this:

"SELECT * FROM TABLE WHERE col IN (" + ",".join(["?"] * len(lst)) + ")"