2012-10-04 72 views
0

我使用此代码从光标获取计数。在执行SQLite中经理选择查询我收到数为6使用Sqlite数据库时获取CursorIndexOutOfBoundsException

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

在此功能,我发现了以下错误的执行: -

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

为什么我得到这个错误?我错过了什么。

+0

可能是你的名字与db-column的值不匹配,你的db-col可能包含空格。 – Lucifer

+2

在问这个问题之前会稍微搜索一下,很快就会给你答案 – njzk2

回答

3

呼叫cursor.moveToFirst()之前使用Cursor

// ... 
cursor.moveToFirst(); 
return cursor.getInt(cursor.getColumnIndex("_count")); 
0

呼叫cursor.moveToFirst()来解决该问题。