0
我想爲我的代碼做一個特定的功能。假設在我的數據庫中有一個叫做tandoori chicken的條目。如何編碼SQL部分,以便我可以用雞唐杜裏過濾數據庫,而不僅僅是固定在唐杜裏雞?SQLite的子串Android
public class MyDatabase extends SQLiteAssetHelper {
private static final String DATABASE_NAME = "FoodDatabase1.sqlite";
private static final int DATABASE_VERSION = 1;
public MyDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
和getFood
函數。
/*
* Receive searchQuery in string form
* return Cursor object
*/
public Cursor getFood(String searchQuery) {
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
String [] sqlSelect = {"_id", "FOOD", "CALORIES"};
String sqlTables = "fooddb1";
String whereClause=null;
String[] whereArgs=null;
/*
*if searchQuery is empty then null will be passed in query() function which
*will display all rows
*if searchQuery is not null it will look for match in the table
*/
if(!searchQuery.equals("")){
whereClause="Food LIKE ?";
/*
*LIKE statement will look for substring in the table if found it will add that row to cursor
*/
whereArgs= new String[] {
"%"+searchQuery+"%"
};
}
qb.setTables(sqlTables);
Cursor c = qb.query(db, sqlSelect, whereClause, whereArgs,
null, null, null);
c.moveToFirst();
return c;
}
這是否意味着對於queryWords數組,我需要定義所有可能的組合? – coffeeboi619
對於帶有IN條款的解決方案,您不必定義所有組合。只需要將它們分成'queryWords'數組。 –
你可以舉一個如何在queryWords數組中分割它們的例子嗎?謝謝! – coffeeboi619