2016-03-16 34 views
0

我希望從sqlite數據庫獲取單行。從基於id的數據庫中獲取單行導致錯誤

我使用下面的代碼來獲取值。

public ShipmentStatusDao getShipmentStatusRow(int id) { 
     SQLiteDatabase db = this.getReadableDatabase(); 

     Cursor cursor = db.query(TABLE_SHIPMENTSTATUS, new String[]{KEY_NOTES, 
         KEY_PHOTOS, KEY_DOCUMENTS}, KEY_ID + "=?", 
       new String[]{String.valueOf(id)}, null, null, null, null); 
     if (cursor != null) 
      cursor.moveToFirst(); 

     ShipmentStatusDao notification = new ShipmentStatusDao(cursor.getInt(0), 
       cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6), cursor.getString(7), cursor.getString(8)); 

     return notification; 
    } 

進出口使用以下代碼來調用上述方法,

ShipmentStatusDao shipmentStatusRow = db.getShipmentStatusRow(1); 
shipmentStatusRow.getDate(); 

林得到以下錯誤,

java.lang.IllegalStateException:無法讀取行0,來自 CursorWindow的列3。確保光標在 從其訪問數據之前正確初始化。 在android.database.CursorWindow.nativeGetString(本機方法) 在android.database.CursorWindow.getString(CursorWindow.java:438) 在android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51) 在com.vaighai .handlers.NotificationDbHandler.getShipmentStatusRow在(NotificationDbHandler.java:364) 在com.vaighai.fragments.StatusFragment $ 5.onItemClick(StatusFragment.java:237) 在android.widget.AdapterView.performItemClick(AdapterView.java:301) android.widget.AbsListView.performItemClick(AbsListView.java:1510) 在android.widget.AbsListView $ PerformClick.run(AbsListView.java:3339) at android.os.Handler.handleCallback(Handler.java:730) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:176) at android.app.ActivityThread.main(ActivityThread.java:5419) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com .android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1046) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862) at dalvik.system.NativeStart.main(Native方法)03-16 14:54:32.826 2337-7411 /? I/ActivityManager:通知應用程序崩潰

我該如何才能糾正上述錯誤?

+0

'如果(指針!= NULL){//你的代碼}'喜歡這種改變將幫助你 – Sree

回答

0

你只檢查您的Cursor爲空,但你不檢查它是否是空的

moveToFirst()返回boolean所以你知道它是空的。您可以通過查詢提供new String[]{KEY_NOTES, KEY_PHOTOS, KEY_DOCUMENTS}改變這種

if (cursor != null) 
     cursor.moveToFirst(); 

ShipmentStatusDao notification = new ShipmentStatusDao(cursor.getInt(0), 
      cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6), cursor.getString(7), cursor.getString(8)); 

return notification; 

進入這個

if (cursor != null && cursor.moveToFirst()) { 
    ShipmentStatusDao notification = new ShipmentStatusDao(cursor.getInt(0), 
      cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6), cursor.getString(7), cursor.getString(8)); 

    cursor.close(); // Don't forget to close BTW 
    return notification; 
} else { 
    // Cursor is null or empty, you need to do something with this 
    return null; 
} 

UPDATE
此外,你只有三列在您的選擇。如果您想將其更改爲null,則Cursor將包含所有列。這應該可以解決您的錯誤

+0

我嘗試過,但同樣的錯誤再次被拋出 – user1241241

+0

這是因爲你選擇列。更新了我的回答 – 0xDEADC0DE

+0

,但它的工作原理卻像[email protected] – user1241241

3

您只查詢三個字段(KEY_NOTES, KEY_PHOTOS,KEY_DOCUMENTS)。所以cursor.getString(3)會拋出錯誤。

0

所有與遊標相關的代碼/查詢都應該包含在一個代碼塊中。

Cursor cursor = db.query(TABLE_SHIPMENTSTATUS, new String[]{KEY_NOTES, 
         KEY_PHOTOS, KEY_DOCUMENTS}, KEY_ID + "=?", 
       new String[]{String.valueOf(id)}, null, null, null, null); 
if (cursor != null) { 
    while (cursor.moveToNext()) { 
    ... // simply do this 
    } 
} 

希望這有助於!