0

我正在运行以下查询:

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));

它工作正常。

谁能告诉我这里出了什么问题以及如何解决?

4

2 回答 2

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)));
于 2013-03-16T04:44:32.147 回答
1

您直接尝试从光标访问 3 列,但您的第三列是计数,并且您没有为该列指定别名,这就是您收到此错误的原因。只需尝试输入列名的别名,然后尝试按如下方式访问它:

替换此行

   + MySQLiteHelper.COLUMN_PARENT + ", count(*) from "

经过

    + MySQLiteHelper.COLUMN_PARENT + ", count(*) as total from "
于 2013-03-16T04:49:14.997 回答