Reputation: 119
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
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