Setting multiple Selections args in SQLite database query

Mr. Pivirotto picture Mr. Pivirotto · Sep 14, 2012 · Viewed 12.7k times · Source

How would i go about querying more than one selection arg? For example here is how my database is formatted

my database

Here is the code i used to search with just one seletion arg:

public Cursor getType(String type) throws SQLException 
{
    Cursor mCursor =
            db.query(true, DB_TABLE, new String[] {
                    KEY_ROWID,
                    KEY_ALCOHOL, 
                    KEY_TYPE,
                    KEY_BRAND,
                    KEY_PRICE
                    }, 
                    KEY_TYPE + "=?", 
                    new String[] { type },
                    null, 
                    null, 
                    null, 
                    null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

But this only searches by KEY_TYPE, how would I set it so it searches by KEY_TYPE, KEY_ALCOHOL, and KEY_PRICE?

Answer

Rajendra picture Rajendra · Sep 14, 2012

This may help you

  public Cursor getType(String type) throws SQLException 
    {
     Cursor mCursor =
        db.query(true, DB_TABLE, new String[] {
                KEY_ROWID,
                KEY_ALCOHOL, 
                KEY_TYPE,
                KEY_BRAND,
                KEY_PRICE
                }, 
                KEY_TYPE + "=?" + " AND " + KEY_ALCOHOL + "=?" " AND " + KEY_PRICE + "=?", 

                new String[] { type,alcohol,price },
                null, 
                null, 
                null, 
                null);
if (mCursor != null) {
    mCursor.moveToFirst();
 }
 return mCursor;
}