如何在单个查询中选择多个值

时间:2010-12-09 14:52:48

标签: android

我正在使用sqllite作为我的搜索模块。下面的代码选择单个状态值。我可以在单个查询中选择多个状态值.........

这是我的代码:

 String sta=Search.status2;
 String doc=Search.document;
 String[] items=sta.split(",");
 Cursor dbcur = myDB.rawQuery("select * from "+TABLE_NAME+" where Status='"+items[i]+"'AND Ball_in='"+ball+"'", null);
 if(dbcur.moveToFirst())
     {
      int k=0;
      System.out.println("enter first");
      do
        {

      String title1=dbcur.getString(dbcur.getColumnIndex("Title"));
      System.out.println("title"+title1);
      String name1=dbcur.getString(dbcur.getColumnIndex("Name"));
      System.out.println("name"+name1);
      String open1=dbcur.getString(dbcur.getColumnIndex("Open"));
      System.out.println("open"+open1);
      String close1=dbcur.getString(dbcur.getColumnIndex("Close"));
      System.out.println("close"+close1);
      String no1=dbcur.getString(dbcur.getColumnIndex("No"));
      System.out.println("no"+no1);
      no.add(no1+","+k);
   first.put(no1+","+k, title1);
   second.put(no1+","+k,name1);
   third.put(no1+","+k, open1);
   fourth.put(no1+","+k,close1);
   k=k+1; 
   mylist=sorting(no,1,true); 
    }while(dbcur.moveToNext());
     }
     dbcur.close();
  myDB.close();

1 个答案:

答案 0 :(得分:1)

您使用items[i]作为状态参数。创建一个循环,它通过参数并将它们附加到您的查询中。

String[] items=sta.split(",");

String statusFilter = " Status IN (" + items[0];
StringBuilder builder = new StringBuilder(statusFilter);
for (int i = 1; i < items.length; i++) {
  builder.append(", ");
  builder.append(items[i]);
}
builder.append(')');

 Cursor dbcur = myDB.rawQuery("select * from "+TABLE_NAME+" where " + builder.toString() + "'AND Ball_in='"+ball+"'", null);