{"record":{"id":"8fbfc6d6f5c10601","repo":"greenrobot/greenDAO","slug":"no-result-for-count","errorCode":null,"errorMessage":"No result for count","messagePattern":"No result for count","errorType":"exception","errorClass":"DaoException","httpStatus":null,"severity":"error","filePath":"DaoCore/src/main/java/org/greenrobot/greendao/query/CountQuery.java","lineNumber":61,"sourceCode":"\n    private final QueryData<T> queryData;\n\n    private CountQuery(QueryData<T> queryData, AbstractDao<T, ?> dao, String sql, String[] initialValues) {\n        super(dao, sql, initialValues);\n        this.queryData = queryData;\n    }\n\n    public CountQuery<T> forCurrentThread() {\n        return queryData.forCurrentThread(this);\n    }\n\n    /** Returns the count (number of results matching the query). Uses SELECT COUNT (*) sematics. */\n    public long count() {\n        checkThread();\n        Cursor cursor = dao.getDatabase().rawQuery(sql, parameters);\n        try {\n            if (!cursor.moveToNext()) {\n                throw new DaoException(\"No result for count\");\n            } else if (!cursor.isLast()) {\n                throw new DaoException(\"Unexpected row count: \" + cursor.getCount());\n            } else if (cursor.getColumnCount() != 1) {\n                throw new DaoException(\"Unexpected column count: \" + cursor.getColumnCount());\n            }\n            return cursor.getLong(0);\n        } finally {\n            cursor.close();\n        }\n    }\n\n    // copy setParameter methods to allow easy chaining\n    @Override\n    public CountQuery<T> setParameter(int index, Object parameter) {\n        return (CountQuery<T>) super.setParameter(index, parameter);\n    }\n\n    @Override","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/greenrobot/greenDAO/blob/0bbb338e17c4cf28aba5aae62d42f73f50186157/DaoCore/src/main/java/org/greenrobot/greendao/query/CountQuery.java#L43-L79","documentation":"count() runs the SELECT COUNT(*) query and expects exactly one row with one column. If the cursor cannot advance to the first row, no count exists — a contract violation — so DaoException(\"No result for count\") is thrown.","triggerScenarios":"The underlying rawQuery returns an empty cursor, which should be impossible for a COUNT(*) aggregate; typically caused by an invalid/corrupted SQL statement, a wrapped (non-aggregate) SQL, or a database driver anomaly.","commonSituations":"Custom count queries built via QueryBuilder that were modified to non-aggregate selects; database corruption; intercepted/proxied database layers returning empty result sets.","solutions":["Verify the query SQL is a proper SELECT COUNT(*) statement (inspect query.getSql() if available)","Rebuild the CountQuery from queryBuilder().buildCount() to guarantee aggregate SQL","Check database integrity (e.g. PRAGMA integrity_check) if empty cursors persist"],"exampleFix":"// before\nQuery<User> q = userDao.queryBuilder().where(...).build(); // not a count query\ncustomCount(q);\n// after\nCountQuery<User> cq = userDao.queryBuilder().where(...).buildCount();\nlong n = cq.count();","handlingStrategy":"try-catch","validationCode":"try (Cursor c = db.rawQuery(countSql, params)) {\n    if (!c.moveToFirst()) throw new IllegalStateException(\"count query returned no rows: \" + countSql);\n}","typeGuard":null,"tryCatchPattern":"try {\n    long n = countQuery.count();\n} catch (DaoException e) {\n    if (e.getMessage().equals(\"No result for count\")) {\n        // rebuild query or check DB integrity\n        countQuery = dao.queryBuilder().where(...).buildCount();\n    }\n}","preventionTips":["Use buildCount() to create count queries","Never modify the generated count SQL","Monitor database health; investigate empty cursors"],"tags":["greendao","orm","database","count"],"backgroundTag":"empty-result-set","analyzedSha":"0bbb338e17c4cf28aba5aae62d42f73f50186157","analyzedAt":"2026-09-08T03:22:36.050Z","contentChangedAt":"2026-09-08T03:22:36.050Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}