How to get row count in sqlite using Android?

前端 未结 9 1822
渐次进展
渐次进展 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: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;
    }   
    

提交回复
热议问题