greenrobot/greenDAO · error · DaoException

No result for count

Error message

No result for count

What it means

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.

Solutions

  1. Verify the query SQL is a proper SELECT COUNT(*) statement (inspect query.getSql() if available)
  2. Rebuild the CountQuery from queryBuilder().buildCount() to guarantee aggregate SQL
  3. Check database integrity (e.g. PRAGMA integrity_check) if empty cursors persist

Example fix

// before
Query<User> q = userDao.queryBuilder().where(...).build(); // not a count query
customCount(q);
// after
CountQuery<User> cq = userDao.queryBuilder().where(...).buildCount();
long n = cq.count();
Defensive patterns

Strategy: try-catch

Validate before calling

try (Cursor c = db.rawQuery(countSql, params)) {
    if (!c.moveToFirst()) throw new IllegalStateException("count query returned no rows: " + countSql);
}

Try / catch

try {
    long n = countQuery.count();
} catch (DaoException e) {
    if (e.getMessage().equals("No result for count")) {
        // rebuild query or check DB integrity
        countQuery = dao.queryBuilder().where(...).buildCount();
    }
}

Prevention

When it happens

Trigger: 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.

Common situations: Custom count queries built via QueryBuilder that were modified to non-aggregate selects; database corruption; intercepted/proxied database layers returning empty result sets.

Understand the failure class

Background: EmptyResultError / "no results found": when an API or scraper succeeds but returns zero rows — this error's family across 9 libraries.

Related errors


AI-assisted analysis of greenrobot/greenDAO@0bbb338e17 (2026-09-08). Data as JSON: /api/errors/8fbfc6d6f5c10601. Report an issue: GitHub.

Appendix: source

Thrown at DaoCore/src/main/java/org/greenrobot/greendao/query/CountQuery.java:61

    private final QueryData<T> queryData;

    private CountQuery(QueryData<T> queryData, AbstractDao<T, ?> dao, String sql, String[] initialValues) {
        super(dao, sql, initialValues);
        this.queryData = queryData;
    }

    public CountQuery<T> forCurrentThread() {
        return queryData.forCurrentThread(this);
    }

    /** Returns the count (number of results matching the query). Uses SELECT COUNT (*) sematics. */
    public long count() {
        checkThread();
        Cursor cursor = dao.getDatabase().rawQuery(sql, parameters);
        try {
            if (!cursor.moveToNext()) {
                throw new DaoException("No result for count");
            } else if (!cursor.isLast()) {
                throw new DaoException("Unexpected row count: " + cursor.getCount());
            } else if (cursor.getColumnCount() != 1) {
                throw new DaoException("Unexpected column count: " + cursor.getColumnCount());
            }
            return cursor.getLong(0);
        } finally {
            cursor.close();
        }
    }

    // copy setParameter methods to allow easy chaining
    @Override
    public CountQuery<T> setParameter(int index, Object parameter) {
        return (CountQuery<T>) super.setParameter(index, parameter);
    }

    @Override

View on GitHub (pinned to 0bbb338e17)