2012-08-29 68 views
0

我有五列,我想從五列中搜索數據,但列值可能超過5,它不是固定的。如何使用查詢在sqlite中的多列使用多個值?

列名

第一姓,名,主題,因此,等級。

我想尋找更比一個值使用SQLite。(第一名稱可以是A,B,C,d)

如何實現這一目標?

任何幫助,將不勝感激。

回答

2

在SQLite中使用IN關鍵字將允許您使用一組多值執行搜索。例如:

SELECT * FROM table WHERE first-name IN ('a', 'b', 'c', 'd'); 

現在你可以做的是使用任何環路建立一個輸入(可搜索的字)的參數,並在括號IN更換。

例如:

String names = "'a', 'b', 'c', 'd'";  /* build it through loop */ 

Cursor c = db.rawQuery("SELECT * FROM table WHERE first-name IN (?)", 
       new String[]{names}); 

多列:

String names = "'a', 'b', 'c', 'd'";  /* build it through loop */ 

Cursor c = db.rawQuery("SELECT * FROM table WHERE first-name IN (?) 
     OR last-name IN (?) OR subject IN (?) OR result IN (?) OR grade IN (?)", 
     new String[]{names, names, names, names, names}); 
+0

我想用多列不僅僅是一列 – moDev

+0

然後做相同的多個列使用'OR'關鍵字。看到我更新的答案 – waqaslam

+0

它的工作單列值多列,可能是我沒有正確使用多個值。 String names =「'Zac',」Peter「」;這個可以嗎??我如何在selectionArgs中使用多個字符串數組? – moDev

0
String[] names = new String[3]; 
String QueryArgs =""; 
for(int index=0;index < 3;index++) 
{ 
     QueryArgs = QueryArgs.concat(",?"); 
     names [index] = "xyz"; 
} 
QueryArgs = QueryArgs.substring(1); 

Cursor dataset = db.rawquery("select * from table where name in(" +QueryArgs+ ")",names); 
相關問題