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

后端 未结 9 652
轮回少年
轮回少年 2020-12-16 09:53

all I want to do is send a query like

SELECT * FROM table WHERE col IN (110, 130, 90);

So I prepared the following statement



        
相关标签:
9条回答
  • 2020-12-16 10:40

    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);
    
    0 讨论(0)
  • 2020-12-16 10:42

    Working on a same functionality lead me to this approach: (nodejs, es6, Promise)

        var deleteRecords = function (tblName, data) {
            return new Promise((resolve, reject) => {
                var jdata = JSON.stringify(data);
                this.run(`DELETE FROM ${tblName} WHERE id IN (?)`, jdata.substr(1, jdata.length - 2), function (err) {
                    err ? reject('deleteRecords failed with : ' + err) : resolve();
                });
            });
        };
    
    0 讨论(0)
  • 2020-12-16 10:42

    For example, if you want the sql query:

    select * from table where col in (110, 130, 90)
    

    What about:

    my_list = [110, 130, 90]
    my_list_str = repr(my_list).replace('[','(').replace(']',')') 
    cur.execute("select * from table where col in %s" % my_list_str )
    
    0 讨论(0)
提交回复
热议问题