热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

SQLite:将值列表绑定到“WHEREcolIN(:PRM)”-SQLite:bindlistofvaluesto“WHEREcolIN(:PRM)”

allIwanttodoissendaquerylike我想做的就是发送一个查询SELECT*FROMtableWHEREcolIN(110,130,90);

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

所以我准备了以下声明

SELECT * FROM table WHERE col IN (:LST);

Then I use

然后我用

sqlite_bind_text(stmt, 1, "110, 130, 90", -1, SQLITE_STATIC);

Unfortunately this becomes

不幸的是,这变成

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

and is useless (note the two additional single quotes). I already tried putting extra ' in the string but they get escaped. I didn't find an option to turn off the escaping or prevent the text from being enclosed by single quotes. The last thing I can think of is not using a prepared statement, but I'd only take it as last option. Do you have any ideas or suggestions?

并且没用(请注意另外两个单引号)。我已经尝试在字符串中添加额外的'但是它们会被转义。我没有找到关闭转义或防止文本被单引号括起来的选项。我能想到的最后一件事是没有使用准备好的声明,但我只把它作为最后一个选项。你有什么想法或建议吗?

Thanks

谢谢

Edit:

编辑:

The number of parameters is dynamic, so it might be three numbers, as in the example above, one or twelve.

参数的数量是动态的,因此它可能是三个数字,如上例所示,一个或十二个。

7 个解决方案

#1


24  

You can dynamically build a parameterized SQL statement of the form

您可以动态构建表单的参数化SQL语句

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

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

然后为每个“?”调用一次sqlite_bind_int你添加到声明中。

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.

#2


9  

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);

#3


5  

Even simpler, build your query like this:

更简单,像这样构建您的查询:

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

#4


0  

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

使用相同的功能引导我采用这种方法:(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();            });        });    };

#5


0  

For example, if you want the sql query:

例如,如果您想要sql查询:

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 )

#6


0  

this works fine aswell (Javascript ES6):

这很好用(Javascript ES6):

let myList = [1, 2, 3];`SELECT * FROM table WHERE col IN (${myList.join()});`

#7


0  

A much simpler and safer answer simply involves generating the mask (as opposed to the data part of the query) and allowing the SQL-injection formatter engine to do its job.

一个更简单,更安全的答案只涉及生成掩码(与查询的数据部分相对)并允许SQL注入格式化程序引擎完成其工作。

Suppose we have some ids in an array, and some cb callback:

假设我们在数组中有一些id,还有一些cb回调:

/* we need to generate a '?' for each item in our mask */const mask = Array(ids.length).fill('?').join();db.get(`  SELECT *    FROM films f   WHERE f.id      IN (${mask})`, ids, cb);

推荐阅读
author-avatar
瀑布下的鱼
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有