{"record":{"id":"a1f574830d1fca0f","repo":"greenrobot/greenDAO","slug":"unexpected-column-count","errorCode":null,"errorMessage":"Unexpected column count: ","messagePattern":"Unexpected column count: ","errorType":"exception","errorClass":"DaoException","httpStatus":null,"severity":"error","filePath":"DaoCore/src/main/java/org/greenrobot/greendao/query/CountQuery.java","lineNumber":65,"sourceCode":"        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\n    public CountQuery<T> setParameter(int index, Date parameter) {\n        return (CountQuery<T>) super.setParameter(index, parameter);\n    }\n","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/greenrobot/greenDAO/blob/0bbb338e17c4cf28aba5aae62d42f73f50186157/DaoCore/src/main/java/org/greenrobot/greendao/query/CountQuery.java#L47-L83","documentation":"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.","triggerScenarios":"The underlying select returns multiple columns, e.g. SELECT COUNT(*), x, or a select list including entity columns instead of a single aggregate.","commonSituations":"Hand-modified count SQL; reusing select-query SQL for counting; custom DAO/database wrappers that add columns.","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"],"exampleFix":"// before\n\"SELECT COUNT(*), name FROM USER\"\n// after\n\"SELECT COUNT(*) FROM USER\"","handlingStrategy":"try-catch","validationCode":"try (Cursor c = db.rawQuery(countSql, params)) {\n    c.moveToFirst();\n    if (c.getColumnCount() != 1) throw new IllegalStateException(\"count SQL must select exactly one column\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    long n = countQuery.count();\n} catch (DaoException e) {\n    if (e.getMessage().startsWith(\"Unexpected column count\")) {\n        // fix SQL to select only COUNT(*)\n    }\n}","preventionTips":["Select only COUNT(*) in count queries","Don't reuse entity-select SQL for counting","Use buildCount() for guaranteed aggregate shape"],"tags":["greendao","orm","database","count"],"backgroundTag":"unexpected-response-shape","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"}