How to get row count in sqlite using Android?

前端 未结 9 1798
渐次进展
渐次进展 2020-12-04 16:36

I am creating task manager. I have tasklist and I want when I click on particular tasklist name if it empty then it goes on Add Task activity but if it has 2 or 3 tasks then

相关标签:
9条回答
  • 2020-12-04 16:43

    I know it is been answered long time ago, but i would like to share this also:

    This code works very well:

    SQLiteDatabase db = this.getReadableDatabase();
    long taskCount = DatabaseUtils.queryNumEntries(db, TABLE_TODOTASK);
    

    BUT what if i dont want to count all rows and i have a condition to apply?

    DatabaseUtils have another function for this: DatabaseUtils.longForQuery

    long taskCount = DatabaseUtils.longForQuery(db, "SELECT COUNT (*) FROM " + TABLE_TODOTASK + " WHERE " + KEY_TASK_TASKLISTID + "=?",
             new String[] { String.valueOf(tasklist_Id) });
    

    The longForQuery documentation says:

    Utility method to run the query on the db and return the value in the first column of the first row.

    public static long longForQuery(SQLiteDatabase db, String query, String[] selectionArgs)
    

    It is performance friendly and save you some time and boilerplate code

    Hope this will help somebody someday :)

    0 讨论(0)
  • 2020-12-04 16:44

    c.getCount() returns 1 because the cursor contains a single row (the one with the real COUNT(*)). The count you need is the int value of first row in cursor.

    public int getTaskCount(long tasklist_Id) {
    
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor= db.rawQuery(
            "SELECT COUNT (*) FROM " + TABLE_TODOTASK + " WHERE " + KEY_TASK_TASKLISTID + "=?",
             new String[] { String.valueOf(tasklist_Id) }
        );
        int count = 0;
        if(null != cursor)
            if(cursor.getCount() > 0){
              cursor.moveToFirst();    
              count = cursor.getInt(0);
            }
            cursor.close();
        }
    
        db.close();
        return count;
    }   
    
    0 讨论(0)
  • 2020-12-04 16:54

    Once you get the cursor you can do

    Cursor.getCount()
    
    0 讨论(0)
  • 2020-12-04 16:56

    In order to query a table for the number of rows in that table, you want your query to be as efficient as possible. Reference.

    Use something like this:

    /**
     * Query the Number of Entries in a Sqlite Table
     * */
    public long QueryNumEntries()
    {
        SQLiteDatabase db = this.getReadableDatabase();
        return DatabaseUtils.queryNumEntries(db, "table_name");
    }
    
    0 讨论(0)
  • 2020-12-04 17:03

    Use android.database.DatabaseUtils to get number of count.

    public long getTaskCount(long tasklist_Id) {
            return DatabaseUtils.queryNumEntries(readableDatabase, TABLE_NAME);
    }
    

    It is easy utility that has multiple wrapper methods to achieve database operations.

    0 讨论(0)
  • 2020-12-04 17:05

    Using DatabaseUtils.queryNumEntries():

    public long getProfilesCount() {
        SQLiteDatabase db = this.getReadableDatabase();
        long count = DatabaseUtils.queryNumEntries(db, TABLE_NAME);
        db.close();
        return count;
    }
    

    or (more inefficiently)

    public int getProfilesCount() {
        String countQuery = "SELECT  * FROM " + TABLE_NAME;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        int count = cursor.getCount();
        cursor.close();
        return count;
    }
    

    In Activity:

    int profile_counts = db.getProfilesCount();
        db.close();
    
    0 讨论(0)
提交回复
热议问题