Çağatay Aktaş
Çağatay Aktaş

Reputation: 97

How Can I find DB length in Android DB Table?

I am a new on Android and at now I add some Information on database. Finally I want to learn How many Items do I have in my actually table? How can I do? Thanks

Upvotes: 1

Views: 2363

Answers (3)

Jose Reyes
Jose Reyes

Reputation: 41

public int getAll(){

    Cursor cursor = database.query(TABLE_NAME, new String[] {_ID,NAME,
            EMAIL}, null, null, null, null, null);

    int count = cursor.getCount();

    return count;
}

Upvotes: 2

SVS
SVS

Reputation: 200

You may use a database util queryNumEntries (SQLiteDatabase db, String table). The description is here

Upvotes: 4

Tobias Moe Thorstensen
Tobias Moe Thorstensen

Reputation: 8981

This is how I count all of my rows in my SQLite database.

public int countCases() {

        String SQLQuery = "SELECT COUNT(" + KEY_ID + ") FROM " + TABLE_CASES + ";";
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(SQLQuery, null);
        cursor.moveToFirst();
        int count = cursor.getInt(0);
        cursor.close();
        db.close();
        return count;
    }

Note that KEY_ID and TABLE_CASES are constant values, also KEY_ID is the primary key in my database

Upvotes: 0

Related Questions