Question

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?

Was it helpful?

Solution 2

Figured it out!! Thanks to @Rajendra for giving me some code to build off of! You can only add Strings into the selections args so i did this:

public Cursor getTest(String alcohol, String type, long price) throws SQLException 
{
    Cursor mCursor =
            myDataBase.query(true, DB_TABLE, new String[] {
                    KEY_ROWID,
                    KEY_ALCOHOL, 
                    KEY_TYPE,
                    KEY_BRAND,
                    KEY_PRICE
                    }, 
                    KEY_ALCOHOL + "=?" + " AND " + KEY_TYPE + "=?" + " AND " + KEY_PRICE + "<=" + price,
                    new String[] { alcohol, type},
                    null, 
                    null, 
                    null, 
                    null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

And it works!!

OTHER TIPS

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top