2012-10-04 76 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()來解決該問題。