0

I am wondering, my codes run but it doesn't return results as what I want. For example the data on my

column1 is : hotdog,bacon,cheese

and I want to search bacon. like query cant find bacon. but if I searched cheese or hotdog like query can find it. I think if the word surrounds with a comma. What do I need to do?

public ArrayList<String> getrecom(int recom1) {
    ArrayList<String> result = new ArrayList<String>();

    SQLiteDatabase db = dbHelperFoods.getWritableDatabase();
    String selectQuery = "SELECT * FROM "+ recom.TABLE+" WHERE " +KEY_recom+ " LIKE '%"+recom1+"%'";
    Cursor cursor = db.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        result.add(cursor.getString(cursor.getColumnIndex("name")));
        while (cursor.moveToNext()) {
            result.add(cursor.getString(cursor.getColumnIndex("name")));
        }
    }
    cursor.close();
    db.close();
    return result;
}
Pang
  • 8,605
  • 144
  • 77
  • 113
  • the method signature states that `recom1` is an `int` ... maybe it's not what you where expecting for ?! Side notes : "key" columns which is a string, that's usually a "no no", try reading about `Prepared Statements` https://developer.android.com/reference/java/sql/PreparedStatement.html or http://stackoverflow.com/questions/433392/how-do-i-use-prepared-statements-in-sqlite-in-android , in order to prevet SQL-Injections ... just a friendly reminder of the best practices – Lohmar ASHAR Apr 26 '17 at 23:52
  • No sir, sorry for complications on my example and codes. I modified it to return something. Yes it returns but the thing is, if the data is sorrounded by commas for example "1,2,3,4" my query can find the 1 and 4 while 2 and 3 cant. – John Mark Delos Reyes Apr 27 '17 at 02:24

0 Answers0