Jolo Teneza
Jolo Teneza

Reputation: 33

How to put multiple where clause in a query?

I have this code below in which gets all data having the 'grpnumber'. my question is: How can I get 2 where clause? i want to get the records having the group number and calories?

String[] projection = {
            FoodReaderContract.FoodGroupEntry._ID,
            FoodReaderContract.FoodGroupEntry.COLUMN_GROUP_NUMBER,
            FoodReaderContract.FoodGroupEntry.COLUMN_GROUP_FOODNAME,
            FoodReaderContract.FoodGroupEntry.COLUMN_GROUP_TIMEOFDAY,
            FoodReaderContract.FoodGroupEntry.COLUMN_GROUP_CALORIE
    };

    // Filter results WHERE "title" = 'My Title'
    String selection = FoodReaderContract.FoodGroupEntry.COLUMN_GROUP_NUMBER 
+ " = ?";
    String[] selectionArgs = {grpnumber};

    //String selection = FeedReaderContract.FooEntry._ID + " = ?";_
    //Int[]  selectionArgs = {2};

    // How you want the results sorted in the resulting Cursor
    String sortOrder =
            FoodReaderContract.FoodGroupEntry.COLUMN_GROUP_NUMBER + " DESC";

    Cursor cursor = db.query(
            FoodReaderContract.FoodGroupEntry.GROUP_TABLE_NAME,   // The table to query
            projection,           // The columns to return
            selection,            // The columns for the WHERE clause
            selectionArgs,        // The values for the WHERE clause
                null,             // don't group the rows
            null,                 // don't filter by row groups
            sortOrder             // The sort order
    );

Upvotes: 0

Views: 297

Answers (1)

EpicPandaForce
EpicPandaForce

Reputation: 81588

You can use the AND keyword.

String selection = FoodReaderContract.FoodGroupEntry.COLUMN_GROUP_NUMBER + " = ? "  
  + " AND " + FoodReaderContract.FoodGroupEntry.COLUMN_GROUP_CALORIE + " = ?";
String[] selectionArgs = {String.valueOf(grpnumber), String.valueOf(calorie)};

Upvotes: 1

Related Questions