2012-10-03 249 views
0

我有一個函數從db.but這個總是返回error..i不要在android..looking一些幫助越來越CursorIndexOutOfBoundsException

10-03 11:33:05.870: E/AndroidRuntime(1321): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.projects.myworldsafe/com.projects.myworldsafe.DeailedView}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 

多少知識讀取數據。

String getItemDetailsWp(String id){ 
System.out.print(id); 
SQLiteDatabase db = this.getReadableDatabase(); 
String[] params=new String[]{id}; 
String selectQuery = "SELECT Content FROM " + TABLE_ITEMS +" WHERE "+ITEM_ID+"=?"; 
Cursor c=db.rawQuery(selectQuery,params); 
c.moveToFirst(); 
int index= c.getCount(); 
int indexd= c.getColumnIndex(ITEM_CONTENT); 
System.out.print("Count query"+indexd); 
return c.getString(index); 
} 

回答

2

使用本:

return c.getString(indexd); 

這是你真正想要得到的價值是什麼。您在那裏使用了index,它將返回您的光標中的記錄數。

也能更好的做法是:

String getItemDetailsWp(String id){ 
System.out.print(id); 
SQLiteDatabase db = this.getReadableDatabase(); 
String[] params=new String[]{id}; 
String selectQuery = "SELECT Content FROM " + TABLE_ITEMS +" WHERE "+ITEM_ID+"=?"; 
Cursor c=db.rawQuery(selectQuery,params); 
if(c.getCount()>0){ 
    c.moveToFirst(); 
    int index= c.getCount(); 
    int indexd= c.getColumnIndex(ITEM_CONTENT); 
    System.out.print("Count query"+index);// use index instead of indexd to print total records in query 
    return c.getString(index);// return value of column number specified in indexd 
} 
else 
    return "";// when no records found 
} 
+0

這就是工作thaküalllllll :) –

+0

@ArunMR::)樂意幫助! – Hiral

0

這裏是正確的代碼

int index= c.getCount(); // this will return total rows found 
int indexd= c.getColumnIndex(ITEM_CONTENT); // this is the column index by giving the column name. 
System.out.print("Count query"+indexd); 
return c.getString(indexd); // here you need to pass the column index but you passing the index which was total number of rows 
0
Cursor c=db.rawQuery(selectQuery,params); 

是大小爲0返回光標,改變你的代碼檢查,如果遊標有項目或不,然後移動到第一位:

Cursor c=db.rawQuery(selectQuery,params); 
if(c.getCount()>0) 
{ 
c.moveToFirst(); 
int index= c.getCount(); 
int indexd= c.getColumnIndex(ITEM_CONTENT); 
System.out.print("Count query"+indexd); 
return c.getString(index); 
} 
return ""; 
0

聽起來像你正試圖從一個空的結果集中讀取。

0

檢查你的數據庫,查詢返回一個大小爲0的遊標,所以你試圖訪問索引爲0的遊標,因此錯誤。 重新思考查詢並檢查數據庫中的值。

0

您可能正在訪問0行遊標。更改爲:

// moveToFirst itself will return false if there is no entry on Cursor 
if(c.moveToFirst()){ 
    int cursorCount = c.getCount(); 
    int indexd = c.getColumnIndex(ITEM_CONTENT); 
    System.out.print("Count query"+index); 
    // You should getString(ColumnIndex) not CursorCount 
    return c.getString(indexd); 
} else 
    return ""; 
0

由於您正在傳遞記錄數以代替列號,因此您正在獲取索引越界異常。 作爲最佳編碼實踐,請檢查遊標中的記錄,即它是空的或包含記錄。 使用此

String getItemDetailsWp(String id){ 
System.out.print(id); 
SQLiteDatabase db = this.getReadableDatabase(); 
String[] params=new String[]{id}; 
String selectQuery = "SELECT Content FROM " + TABLE_ITEMS +" WHERE "+ITEM_ID+"=?"; 
Cursor c=db.rawQuery(selectQuery,params); 
if(c.getCount()>0){ 
c.moveToFirst(); 
int index= c.getCount(); 
int indexd= c.getColumnIndex(ITEM_CONTENT); 
System.out.print("Count query"+index); 
return c.getString(indexd); 
} 
else 
return "";// when no records found 
}