admin

Android-使用数组中的值的sqlite in子句

sql

我想执行一个sqlite查询:

select * from table_name where id in (23,343,33,55,43);

in子句中的值需要从字符串数组中获取:

String values[] = {"23","343","33","55","43"}

我该如何实现?


阅读 181

收藏
2021-05-10

共1个答案

admin

我相信一个简单的方法toString()就可以解决问题:

String values[] = {"23","343","33","55","43"};
String inClause = values.toString();

//at this point inClause will look like "[23,343,33,55,43]"
//replace the brackets with parentheses
inClause = inClause.replace("[","(");
inClause = inClause.replace("]",")");

//now inClause will look like  "(23,343,33,55,43)" so use it to construct your SELECT
String select = "select * from table_name where id in " + inClause;
2021-05-10