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

时间:2022-05-05 12:09:16

I want to execute a sqlite query:

我想执行一个sqlite查询:

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

The values in the in clause need to be taken from an array of strings:

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

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

How can I achieve that?

我怎样才能做到这一点?

10X!

10X!

1 个解决方案

#1


34  

I believe a simple toString() will mostly do the trick:

我相信一个简单的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;

#1


34  

I believe a simple toString() will mostly do the trick:

我相信一个简单的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;