2013-05-21 63 views
0

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

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)。但不知道如何检查多个值,请帮助!

+0

请修正[SQL注入](http://en.wikipedia.org/wiki/SQL_injection)的问题。 –

回答

0

使用IN关键字。

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

Here is the basic idea传递数组源码:

// 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个元素。