2016-05-19 18 views
-1

私は1つのテーブルヒストリーを持っています。SQLite android count by名前

履歴テーブルには、私はこのような結果必要前方に私のアプリから私

1 | 12.12.2019 | 88801 

2 | 11.11.2019 | 88802 

3 | 13.1.2019 | 88802 

4 | 1.2.2015 | 88801 

5 | 7.8.2019 | 88801 

名は名前のようであるすべての画像をカウントする必要が3列

ID | timestamp | imagename 

を持っています

Image name| number of images 

8801 | 3 

8802 | 2 

...

転送された変数imageNameでカウントする必要があります。

これは私が

public List<HistoryLog> getAllHistoryLogsForListView(String imageName) { 
     List<HistoryLog> historyLogList = new ArrayList<>(); 
     // Select All Query ORDER BY Timestamp | SORT ASC | LIMIT 150 rows 

     String selectQuery = "SELECT * FROM " + TABLE_HISTORY + " ORDER BY " + KEY_TIMESTAMP + " ASC " + " LIMIT 150"; 


     SQLiteDatabase db = this.getWritableDatabase(); 
     db.beginTransaction(); 
     Cursor cursor = db.rawQuery(selectQuery, null); 

     // looping through all rows and adding to list 
     if (cursor.moveToFirst()) { 
      do { 
       HistoryLog historyLog = new HistoryLog(); 
       historyLog.setId(Integer.parseInt(cursor.getString(0))); 
       historyLog.setTimeStamp(cursor.getString(1)); 
       historyLog.setImageName(cursor.getString(2)); 

       // Adding history logs to list 
       historyLogList.add(historyLog); 
      } while (cursor.moveToNext()); 
     } 

     db.endTransaction(); 

     // return history logs list 
     return historyLogList; 
    } 
+0

あなたが実際に望むものは明確ではありませんか? –

+0

私は何枚の画像が同じ名前を持っているか知りたい – Adnan

答えて

0

今持っているものSELECT IMAGE_NAMEで、HISTORY_TBL GROUP BY IMAGE_NAMEからCOUNT(IMAGE_NAME)

2

使用GROUP BY

SELECT imagename, count(*) FROM History GROUP BY imagename; 

その後、あなたはそれぞれの数を取得することができますがカーソルからの画像:

final Map<Integer, Integer> imageCount = new HashMap<>(); 
// where key is imagename (int) and value is count (int) 
if (c.moveToFirst()) { 
    do { 
     imageCount.put(
      c.getInt(0), 
      c.getInt(1) 
     ); 
    } while (c.moveToNext()); 
}