How to get row count in sqlite using Android?

前端 未结 9 1802
渐次进展
渐次进展 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 17:07

    Do you see what the DatabaseUtils.queryNumEntries() does? It's awful! I use this.

    public int getRowNumberByArgs(Object... args) {
        String where = compileWhere(args);
        String raw = String.format("SELECT count(*) FROM %s WHERE %s;", TABLE_NAME, where);
        Cursor c = getWriteableDatabase().rawQuery(raw, null);
        try {
            return (c.moveToFirst()) ? c.getInt(0) : 0;
        } finally {
            c.close();
        }
    }
    

提交回复
热议问题