2017-06-02 41 views
0

我在下面的代碼中獲取所有具有'grpnumber'的數據。我的問題是:我怎樣才能得到2 where子句?我想獲得組號和卡路里的記錄?如何在查詢中放置多個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

可以使用關鍵字。

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