greenrobot/greenDAO · error · DaoException
Unexpected column count:
Error message
Unexpected column count:
What it means
count() requires the result row to contain exactly one column (the count value). If the cursor reports a column count other than 1, the query did not produce a bare COUNT(*) and DaoException("Unexpected column count: N") is thrown with the actual count.
Solutions
- Ensure the SQL selects only COUNT(*) with no extra columns
- Rebuild the CountQuery with queryBuilder().buildCount()
- Log/inspect cursor.getColumnCount() and the SQL to locate the extra columns
Example fix
// before "SELECT COUNT(*), name FROM USER" // after "SELECT COUNT(*) FROM USER"
Defensive patterns
Strategy: try-catch
Validate before calling
try (Cursor c = db.rawQuery(countSql, params)) {
c.moveToFirst();
if (c.getColumnCount() != 1) throw new IllegalStateException("count SQL must select exactly one column");
} Try / catch
try {
long n = countQuery.count();
} catch (DaoException e) {
if (e.getMessage().startsWith("Unexpected column count")) {
// fix SQL to select only COUNT(*)
}
} Prevention
- Select only COUNT(*) in count queries
- Don't reuse entity-select SQL for counting
- Use buildCount() for guaranteed aggregate shape
When it happens
Trigger: The underlying select returns multiple columns, e.g. SELECT COUNT(*), x, or a select list including entity columns instead of a single aggregate.
Common situations: Hand-modified count SQL; reusing select-query SQL for counting; custom DAO/database wrappers that add columns.
Related errors
- No result for count
- Unexpected row count:
- Expected unique result, but count was
- Cannot delete entity, key is null
- Entity does not exist in the database anymore
AI-assisted analysis of greenrobot/greenDAO@0bbb338e17 (2026-09-08).
Data as JSON: /api/errors/a1f574830d1fca0f.
Report an issue: GitHub.
Appendix: source
Thrown at DaoCore/src/main/java/org/greenrobot/greendao/query/CountQuery.java:65
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
public CountQuery<T> setParameter(int index, Date parameter) {
return (CountQuery<T>) super.setParameter(index, parameter);
}
View on GitHub (pinned to 0bbb338e17)