I'm trying to do a particular function for my code. Suppose, in my database, there is an entry called tandoori chicken. How do I code the SQL part so that I can filter the database with chicken tandoori and not just fixed on tandoori chicken?
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);
}
And the getFood
function.
/*
* 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;
}