2016-05-19 8 views
-1

Ich habe eine Tabelle Geschichte.SQLite Android Zählung nach Name

History-Tabelle hat drei Spalten

ID | timestamp | imagename 

Ich brauche alle Bilder zu zählen, wo Namen wie Name, den ich mich darauf, von meiner app

Beispiel

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 

i wie dies zur Folge haben müssen

Image name| number of images 

8801 | 3 

8802 | 2 

...

Ich muss es durch Weiterleiten Variable ImageName zählen.

Das ist, was ich jetzt

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

nicht klar, was Sie eigentlich wollen? –

+0

Ich möchte wissen, wie viele Bilder denselben Namen haben, ich muss es zählen – Adnan

Antwort

0

SELECT IMAGE_NAME, COUNT (IMAGE_NAME) aus HISTORY_TBL GROUP BY IMAGE_NAME

2

Verwenden GROUP BY haben:

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

Dann können Sie Zahl für jede erhalten Bild vom Cursor:

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