2013-05-21 90 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個元素。