I have a database that holds recipes. I want to be able to search the database for recipes by ingredients. So, if I search for "apple", it should pull up all the recipes that have the word "apple" in the column "ingredients."
Example of table Name: "Apple pie" Ingredients: "apple flour sugar etc."
At first, when it searched the database, I had it set up so that the inputText was just a string. For example, the inputText would be like "apple flour." But, I thought maybe it would make more sense if it took the inputText and put it in a String Array (but I could be totally wrong). So, it would be more like this: ("apple", "flour").
But, my issue is that I don't know how I would write the query so that it looks in the ingredients column for any string that matches one in the array.
Also, should I make it so that the strings in the ingredients column are arrays as well? Because right now they're just strings separated by spaces.
Here's what my query looks like now, which I know isn't right
public Cursor fetchRecipesByName(String inputText) throws SQLException {
SQLiteDatabase mDb = this.getWritableDatabase();
Log.w(TAG, inputText);
Cursor mCursor = null;
if (inputText == null || inputText.length () == 0) {
mCursor = mDb.query(SQLITE_TABLE, new String[] {COLUMN_ROWID,
COLUMN_NAME, COLUMN_TYPE, COLUMN_INGRED, COLUMN_SPECIAL, COLUMN_DESCRIPT, COLUMN_ALLINGRED, COLUMN_INSTRUCT, COLUMN_IMGPATH},
null, null, null, null, null);
}
else {
mCursor = mDb.query(true, SQLITE_TABLE, new String[] {COLUMN_ROWID,
COLUMN_NAME, COLUMN_TYPE, COLUMN_INGRED, COLUMN_SPECIAL, COLUMN_DESCRIPT, COLUMN_ALLINGRED, COLUMN_INSTRUCT, COLUMN_IMGPATH},
COLUMN_INGRED + " like '%" + inputText + "%'",
null, null, null, null, null);
}
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
Aucun commentaire:
Enregistrer un commentaire