0

I'm running the following query:

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

When I read the retrieved data like so:

task.setId(cursor.getLong(0));
task.setTask(cursor.getString(1));
task.setParent(cursor.getLong(2));
task.setChildren(cursor.getInt(3));

I get the following error:

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.

If I remove the line

task.setChildren(cursor.getInt(3));

It works fine.

Can anyone tell me what is wrong here and how to fix it?

1
  • Sorry about this. I just found the error. I had that query in two places and was editing the wrong one. (I left the count(*) off the other one.) Thanks for all your help. All the suggestions worked fine once I realized my mistake. Commented Mar 16, 2013 at 5:32

2 Answers 2

3

Try replacing this line

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

by

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

Edit: Also try replacing

task.setChildren(cursor.getInt(3));

by

task.setChildren((int)cursor.getLong(3));

or may be

task.setChildren(Integer.parseInt(cursor.getString(3)));
Sign up to request clarification or add additional context in comments.

1 Comment

I tried changing the query to say '...", count(*) as children from "...' and using 'n = cursor.getColumnIndex("children");', but I still get the same error.
1

You are directly trying to access the 3 column from the cursor but your third column is the count and you didn't specified the alias for that column that is why you are getting this error. Just try to put the alias of the column name and then try to access it as below:

Replace this line

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

by

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

1 Comment

See what I said in response to @appsroxcom

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.