Trojan
Trojan

Reputation: 119

Get row count from SQLite

I am trying to get a row count from sqlite for my android application but for some reason its always returning Zero even though I have a enough amount of rows in my DB.

Here is the Code:

DatabaseConnector database1 = new DatabaseConnector(getApplicationContext());
int TotalPosts = database1.getIds();


 public int getIds()
       {
           String selectQuery = "SELECT COUNT(Pst_id) AS CountTotal FROM student_posts";           
           SQLiteDatabase database = this.dbOpenHelper.getReadableDatabase();
           Cursor c = database.rawQuery(selectQuery, null);     
           count = c.getColumnIndex("CountTotal");    
           return count;
       }

Thanks.

Upvotes: 0

Views: 11424

Answers (2)

Sagar Maiyad
Sagar Maiyad

Reputation: 12743

Try like below:

public int getIds()
    {
    String selectQuery = "SELECT Pst_id FROM student_posts";           
    SQLiteDatabase database = this.dbOpenHelper.getReadableDatabase();
    Cursor c = database.rawQuery(selectQuery, null);
    c.moveToFirst();
    int total = c.getCount();
    c.close();

    return total;
}

Upvotes: 10

CRUSADER
CRUSADER

Reputation: 5472

Try c.getCount()..

Reference

Hope this helps..

Upvotes: 4

Related Questions