Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 具有多个参数的一个参数-Selection和SelectionArgs_Java_Sqlite_Cursor - Fatal编程技术网

Java 具有多个参数的一个参数-Selection和SelectionArgs

Java 具有多个参数的一个参数-Selection和SelectionArgs,java,sqlite,cursor,Java,Sqlite,Cursor,我遇到了这个问题: Caused by: java.lang.IllegalArgumentException: Cannot bind argument at index 3 because the index is out of range. The statement has 1 parameters. 由于: String where = "id_1 = 50 AND id_2 = ?"; //The String[] is ideally of dynamic length and

我遇到了这个问题:

Caused by: java.lang.IllegalArgumentException: Cannot bind argument at index 3 because the index is out of range. The statement has 1 parameters.
由于:

String where = "id_1 = 50 AND id_2 = ?";
//The String[] is ideally of dynamic length and not necessarily 3 in size.
String[] whereArgs = {"60", "61", "62"};

cursor.setSelection(where);
cursor.setSelectionArgs(whereArgs);
我只是用错了方法。我已经意识到这一点。但我认为这表明了我正在努力实现的目标

我的问题: 有没有一种方法可以将长度和参数不同的数组插入到单个参数中?我错过的任何最佳实践。我只是把自己烤成了一个糟糕的处境吗

我可能要查找的SQL语句:

WHERE id_1 = 50 AND ((id_2 = 60) OR (id_2 = 61) OR (id_2 = 62))
我能想到的解决我的问题的唯一方法是创建一个字符串,并在循环中基于它构建字符串[]的长度,在每次迭代中添加或(id_2=xx)。对我来说,这听起来不是一个很好的解决方案

谢谢你的时间

尝试使用以下代码:

String where = "id_1 = 50 AND (id_2 = ? OR id_2 = ? OR id_2 = ?)";

String[] whereArgs = {"60", "61", "62"};

通常,动态构造WHERE子句是正确的解决方案。使用
StringBuilder.append()
而不是
+
来节省一些字符串构造开销

在某些情况下,您可能还需要发出多个语句。所以不是

select from table where id_1 = 50 AND (id_2 = ? OR id_2 = ? OR id_2 = ?);
你可以做一些


那么()中的MySQL
呢<代码>“…和id_2在(?)
和build
wherergs
中,所以它是一个逗号分隔的字符串。@marekful我没有试过,没有。我会试一试。在这种情况下,这似乎是一种足够灵活的方法,因为您可以轻松地将数组元素加入到CSV列表中。@marekful这很有效。谢谢如果你写一个答案,我可以记下来。我认为这并没有考虑到我的字符串[]可以具有不同的长度。
select from table where id_1 = 50 AND (id_2 = ?);
select from table where id_1 = 50 AND (id_2 = ?);
select from table where id_1 = 50 AND (id_2 = ?);