如何在查询中放置多个where子句?

时间:2017-06-02 16:49:18

标签: android sqlite android-sqlite

我在下面有这个代码,其中所有数据都包含' grpnumber'。我的问题是:如何获得2 where子句?我想获得具有组号和卡路里的记录?

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
    );

1 个答案:

答案 0 :(得分:1)

您可以使用 AND 关键字。

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