0

I'm using this code to get the count from the cursor. On executing the Select query in SQLite Manager I am getting count as 6.

public int getCount(String name){ Cursor cursor; cursor = myDataBase.rawQuery( "select count(*) as _count from tProviderNode where parentId IN(select identifier from tProviderNode where name='" + name + "');", null); return cursor.getInt(cursor.getColumnIndex("_count")); } 

On execution of this function I'm getting the following error:-

android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1 

Why I'm getting this error? Am I missing anything.

2
  • may be your name is not matching with the db-column's value, it may be possible that your db-col contains white space. Commented Oct 4, 2012 at 10:00
  • 2
    a little search before asking the question would have given you the answer in no time Commented Oct 4, 2012 at 10:04

2 Answers 2

3

Call cursor.moveToFirst() before you use the Cursor:

// ... cursor.moveToFirst(); return cursor.getInt(cursor.getColumnIndex("_count")); 
Sign up to request clarification or add additional context in comments.

Comments

0

Call cursor.moveToFirst() to fix the issue.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.