Sqlite检查多个值 - Android

时间:2013-05-21 06:38:50

标签: android sqlite

我有一个要求,我需要在sqlite where子句中检查多个值。查询是这样的。

String sql = "SELECT * FROM _jobcard_table as t1 " + 
    " left join _customer_table as t2 on (t1._custid=t2._custid) " + 
    " left join _make_table as t3 on (t1._job_make=t3._makeid) " + 
    " left join _model_table as t4 on (t1._jobmodel=t4._modid) " + 
    " where _addedby='" + Globals.mid + 
        "' AND _jobestimatedate='"+curr+"' " + 
    " group by _jobregno order by _custname";

这里curr,是一个值的arraylist,我需要检查日期是否是ArrayList中的值之一,然后返回该行,我该怎么做?我可以检查一个值,如curr.get(0)。但不确定如何检查多个值,请帮忙!

2 个答案:

答案 0 :(得分:0)

使用IN关键字。

"_jobedtimatedate IN (\"value1\", \"value2\", \"value3\")"   

答案 1 :(得分:0)

Here is the basic idea用于将数组传递给sqlite:

// Pseudocode
list = "";
separator = "";
for (i=0; i < array.length; i++) {
    list += '?';
    statement.parameters[i] = array[i]; 

    if (i != array.length-1) {
          list += ',';
    }
}
statement.text = "SELECT blah blah blah WHERE media.id IN (" + list + ")";

上面的代码使用占位符来避免SQL注入。创建的SQL类似于SELECT col from table WHERE col IN (?,?,...,?),然后使用SQLiteStatement.bind设置实际值。

大型数组仍然存在问题:代码至少应该检查数组中元素的数量是不是很大,比如100个元素。