-1

I am trying to collect TotalCount from DB but Unable to do so as something is wrong with my query and I am unable to figure it out. Below is my Query:

public int getIds()
               {
                   String strCount = "";
                   int count = 0;
                   Cursor c = database.rawQuery("SELECT COUNT(Pst_id) AS CountTotal FROM student_posts", null);

                   while(c.moveToFirst())
                   {
                   strCount = c.getString(c.getColumnIndex("CountTotal"));
                   }
                   count = Integer.valueOf(strCount).intValue();
                   return count;                       
               }
Trojan
  • 119
  • 2
  • 3
  • 12
  • tip : how many column do you think there should be ? – njzk2 Apr 10 '13 at 13:12
  • first it can be reduced to `count = c.getInt(0);` ... for getting new Pst_id for student_posts table you better make Pst_id Primary INT key and use `int newid = database.insert(..);` instead of `database.rawQuery("INSERT ...")` – Selvin Apr 10 '13 at 13:16

1 Answers1

2

You have an infinite loop:

/* c.moveToFirst() returns true when at least one entry was found */
while (c.moveToFirst()) {
    strCount = c.getString(c.getColumnIndex("CountTotal"));
}

change it to

if (c.moveToFirst())

and don't forget to add some null-check on the curser and close the cursor before you return.

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150