Android 如何从sqlite数据库中获取CountTotal
我试图从数据库中收集TotalCount,但无法这样做,因为我的查询有问题,我无法找到答案。以下是我的疑问:Android 如何从sqlite数据库中获取CountTotal,android,sqlite,android-sqlite,Android,Sqlite,Android Sqlite,我试图从数据库中收集TotalCount,但无法这样做,因为我的查询有问题,我无法找到答案。以下是我的疑问: public int getIds() { String strCount = ""; int count = 0; Cursor c = database.rawQuery("SELECT COUNT(Pst_id) AS CountTotal
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;
}
您有一个无限循环:
/* c.moveToFirst() returns true when at least one entry was found */
while (c.moveToFirst()) {
strCount = c.getString(c.getColumnIndex("CountTotal"));
}
换成
if (c.moveToFirst())
不要忘记在游标上添加一些空检查,并在返回之前关闭游标。提示:您认为应该有多少列?首先可以将其减少为
count=c.getInt(0)代码>。。。要为student_posts表获取新的Pst_id,最好将Pst_id设为主INT键,并使用INT newid=database.insert(…)代码>而不是数据库.rawQuery(“插入…”)