androidsqliteandroid-query

query method in SQLiteDatabase


I want to know how to use query method that have where clause with like property. basically what i want is to select all the columns WHERE C_NAME column is LIKE keyWord.

I've tried this, but it doesn't work:

cursor = db.query(TABLE, null, C_NAME, new String[] {"like '"+keyWord+"'"}, null, null, null);

Solution

  • Look at the docs. They say:

    selection   A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given table.
    

    so try this:

    cursor = db.query(TABLE, C_NAME, new String[] {C_NAME + " like '" + keyWord + "'"}, null, null, null);
    

    Also see this answer as well to see some examples on how to use the selectionArgs (4th argument above). That is where keyWord would have to go.