【问题标题】:How to get CountTotal from sqlite Database [duplicate]如何从 sqlite 数据库中获取 CountTotal [重复]
【发布时间】:2013-04-10 13:11:05
【问题描述】:

我正在尝试从 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;                       
               }

【问题讨论】:

  • 提示:你认为应该有多少列?
  • 首先它可以减少到count = c.getInt(0); ...为了获得student_posts表的新Pst_id,你最好设置Pst_id Primary INT key并使用int newid = database.insert(..);而不是database.rawQuery("INSERT ...")

标签: android sqlite android-sqlite


【解决方案1】:

你有一个无限循环:

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

改成

if (c.moveToFirst())

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

【讨论】:

    猜你喜欢
    • 2018-01-30
    • 2019-10-04
    • 2016-06-23
    • 2016-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多