我正在运行以下查询:
String q = "select " + MySQLiteHelper.COLUMN_ID + ", "
+ MySQLiteHelper.COLUMN_TASK + ", "
+ MySQLiteHelper.COLUMN_PARENT + ", count(*) from "
+ MySQLiteHelper.TABLE_TASKS + " where "
+ MySQLiteHelper.COLUMN_ID + " = " + insertId + " group by "
+ MySQLiteHelper.COLUMN_ID + ", " + MySQLiteHelper.COLUMN_TASK
+ ", " + MySQLiteHelper.COLUMN_PARENT;
Cursor cursor = database.rawQuery(q, null);
当我像这样读取检索到的数据时:
task.setId(cursor.getLong(0));
task.setTask(cursor.getString(1));
task.setParent(cursor.getLong(2));
task.setChildren(cursor.getInt(3));
我收到以下错误:
03-15 22:15:06.078: E/AndroidRuntime(31426):
java.lang.IllegalStateException:
Couldn't read row 0, col 3 from CursorWindow.
Make sure the Cursor is initialized correctly before accessing data from it.
如果我删除该行
task.setChildren(cursor.getInt(3));
工作正常。
有谁能告诉我这里有什么问题以及如何解决?
答案 0 :(得分:3)
尝试替换此行
+ MySQLiteHelper.COLUMN_PARENT + ", count(*) from "
通过
+ MySQLiteHelper.COLUMN_PARENT + ", count(*) as total from "
修改:还可以尝试替换
task.setChildren(cursor.getInt(3));
通过
task.setChildren((int)cursor.getLong(3));
或可能是
task.setChildren(Integer.parseInt(cursor.getString(3)));
答案 1 :(得分:1)
您正在直接尝试从光标访问3列,但您的第三列是计数,并且您未指定该列的别名,这就是您收到此错误的原因。只需尝试输入列名的别名,然后尝试访问它,如下所示:
替换此行
+ MySQLiteHelper.COLUMN_PARENT + ", count(*) from "
通过
+ MySQLiteHelper.COLUMN_PARENT + ", count(*) as total from "