【问题标题】:How to get the row count of a query in Android using SQLite?如何使用 SQLite 在 Android 中获取查询的行数?
【发布时间】:2011-09-15 03:33:18
【问题描述】:

如何使用 SQLite 在 Android 中获取查询的行数?看来我下面的方法不起作用。

public int getFragmentCountByMixId(int mixId) {
    int count = 0;
    SQLiteDatabase db = dbOpenHelper.getWritableDatabase();

    Cursor cursor = db.rawQuery(
        "select count(*) from downloadedFragement where mixId=?",
        new String[]{String.valueOf(mixId)});
    while(cursor.moveToFirst()){
        count = cursor.getInt(0);
    }
    return count;
}    

【问题讨论】:

  • 与问题无关:您不能通过无限期地调用“moveToFirst”来迭代游标。您应该调用一次,然后使用 while(cursor.moveToNext()) { ...

标签: android sqlite count row


【解决方案1】:
val query = "SELECT * FROM $TABLE_NAME ;"
val result = db.rawQuery(query,null)
Toast.makeText(ctx,result.count,Toast.LENGTH_SHORT).show()

【讨论】:

    【解决方案2】:
     public long getRecords() {
        return DatabaseUtils.longForQuery(db, "SELECT COUNT(*) FROM contacts", null);
    }
    

    如果您的数据库使用自动增量,您可以使用它作为方法或使用它

     public long getRecords() {
        return DatabaseUtils.longForQuery(db, "SELECT seq FROM sqlite_sequence", null);
    }
    

    【讨论】:

      【解决方案3】:

      DatabaseUtils

      public static long queryNumEntries(SQLiteDatabase db, String table)
      

      【讨论】:

      • 如果它是一个没有数据库名称的内存数据库怎么办?
      • “DatabaseUtils.queryNumEntries”仍然是一个不错的选择还是已被弃用?
      【解决方案4】:

      这会更有效,因为适用于所有版本:

      int numRows = DatabaseUtils.longForQuery(db, "SELECT COUNT(*) FROM table_name", null);
      

      int numRows = DatabaseUtils.queryNumEntries(db, "table_name");
      

      如果您想获取特定选择的行数,那么您应该使用(在API 11中添加)

      public static long queryNumEntries (SQLiteDatabase db, String table, String selection)
      

      谢谢:)

      【讨论】:

      • 在第一个示例中,您使用 longForQuery()。我认为该方法仅返回第一行第一列中的值。那么这将如何显示数据库中的总行数?是不是因为查询 SELECT COUNT(*) 是在整个数据库上运行的?
      【解决方案5】:
      cursor.moveToNext();
      cursor.getCount();
      

      如果没有调用moveToNext(),可能会出现cursorIndexOutOfBoundException。

      【讨论】:

        【解决方案6】:

        使用 String 代替 int

        String strCount = "";
        int count = 0;
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        
        Cursor cursor = db.rawQuery(
            "select count(*) from downloadedFragement where mixId=?",
            new String[]{String.valueOf(mixId)});
        
        while(cursor.moveToFirst()){
            strCount = cursor.getString(cursor.getColumnIndex("COUNT(*)"));
        }
        count = Integer.valueOf(strCount).intValue();
        

        【讨论】:

          【解决方案7】:

          【讨论】:

          • 我不知道这将如何与您当前的实现一起工作,但如果您只是将 count(*) 更改为 *,这应该可以工作。
          • 我认为最好的解决方案return (int) DatabaseUtils.longForQuery(mDB, "SELECT COUNT(*) FROM table_name", null);
          • 对于有 1 行的游标,这将返回 0。
          【解决方案8】:

          查询表中的_ID 列,然后在游标上调用getCount。这是link 我在我的一个项目中做的。查看第 110 行。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-08-07
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多