SQLite
 sql >> Base de Dados >  >> RDS >> SQLite

Como obter a contagem de linhas no sqlite usando o Android?


Usando DatabaseUtils.queryNumEntries() :
public long getProfilesCount() {
    SQLiteDatabase db = this.getReadableDatabase();
    long count = DatabaseUtils.queryNumEntries(db, TABLE_NAME);
    db.close();
    return count;
}

ou (mais ineficiente )
public int getProfilesCount() {
    String countQuery = "SELECT  * FROM " + TABLE_NAME;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    int count = cursor.getCount();
    cursor.close();
    return count;
}

Em atividade:
int profile_counts = db.getProfilesCount();
    db.close();