根据id从数据库中获取单行会导致错误

时间:2016-03-16 09:31:53

标签: java android sqlite

我希望从sqlite db中获取一行。

我使用以下代码来获取值。

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行,第3列   CursorWindow。确保之前正确初始化了Cursor   从中访问数据。                                                          在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)                                                          在android.os.Handler.handleCallback(Handler.java:730)                                                          在android.os.Handler.dispatchMessage(Handler.java:92)                                                          在android.os.Looper.loop(Looper.java:176)                                                          在android.app.ActivityThread.main(ActivityThread.java:5419)                                                          at java.lang.reflect.Method.invokeNative(Native Method)                                                          在java.lang.reflect.Method.invoke(Method.java:525)                                                          在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1046)                                                          在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)                                                          在dalvik.system.NativeStart.main(Native Method)03-16 14:54:32.826   2337-7411 /? I / ActivityManager:通知ApplicationCrash

我怎样才能纠正上述错误?

3 个答案:

答案 0 :(得分:3)

您只查询三个字段(KEY_NOTES,                         KEY_PHOTOS,KEY_DOCUMENTS)。所以cursor.getString(3)会抛出错误。

答案 1 :(得分:0)

您只检查Cursor是否为空,但不检查它是否为空

moveToFirst()会返回boolean,因此您知道它是空的。你可以改变这个

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

<强>更新
此外,通过在查询中提供new String[]{KEY_NOTES, KEY_PHOTOS, KEY_DOCUMENTS},您的选择中只有三列。如果您将其更改为null,则Cursor包含所有列。这应该可以解决您的错误

答案 2 :(得分: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 
    }
}

希望这有帮助!