【问题标题】:Android: SQLite, How to Select count(*) from multiple tables?Android:SQLite,如何从多个表中选择计数(*)?
【发布时间】:2013-03-06 17:46:11
【问题描述】:

如果我使用光标,我不能在下面的代码中使用“cursor.getString”。 我发现“Cursor.count = 0”。

但是如果我在 adb shell 中执行这段代码,我可以得到结果“10|15|”。

我可以知道原因或如何实现我的目标吗?非常感谢。

select (select count(*) from table1) as count1, (select count(*) from table2) as count2;

【问题讨论】:

    标签: android sql


    【解决方案1】:

    你用光标做什么?你怎么得到它? (内容解析器、执行原始 sql 等)。

    more correct 方法可以进行选择计数,相关链接清楚地显示。

    【讨论】:

      【解决方案2】:

      所以通常使用两个查询

      String[] queries = {"select count(*) from table1",
                          "select count(*) from table2"};
      

      然后使用Cursor 并执行查询和getter 以获取值。

      Cursor c = null;
      int index = 0;
      int totalCount = 0;
      while (index < queries.length) {
         c = db.rawQuery(queries[index], null);
         if (c.moveToFirst()) {
            totalCount += c.getInt(0);
         }
         index++;
      }
      

      【讨论】:

      • 这不会提供两个表中各个行的计数。答案错误。
      猜你喜欢
      • 1970-01-01
      • 2016-12-22
      • 1970-01-01
      • 2010-11-19
      • 1970-01-01
      • 1970-01-01
      • 2013-05-29
      • 1970-01-01
      • 2013-06-23
      相关资源
      最近更新 更多