-1

我正在尝试从 DB 收集 TotalCount 但由于我的查询有问题而无法这样做,我无法弄清楚。以下是我的查询:

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;                       
               }
4

1 回答 1

2

你有一个无限循环:

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

将其更改为

if (c.moveToFirst())

并且不要忘记在光标上添加一些空检查并在返回之前关闭光标。

于 2013-04-10T13:13:16.663 回答