android.database.CursorIndexOutOfBoundsException 仅在桌面版本中

问题描述 投票:0回答:1

我在 Android Studio 中使用 libgdxgdx-sqlite。我想使用以下方式查询我的数据库:

try {
    cursorSettings = dbHandler.rawQuery("SELECT "+COLUMN_SETTING+" FROM "+TABLE_SETTINGS+" WHERE "+COLUMN_SETTINGSID+" =13");
    System.out.println(cursorSettings.getLong(0));
} catch (SQLiteGdxException e) {
    e.printStackTrace();
}

桌面版本可以工作,但在 Android 版本中我得到:

05-25 10:21:47.341 11978-12232/ch.shuttering.rapporte E/AndroidRuntime: FATAL EXCEPTION: GLThread 82275
Process: ch.shuttering.rapporte, PID: 11978
android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
    at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:74)
    at com.badlogic.gdx.sqlite.android.AndroidCursor.getLong(AndroidCursor.java:61)
    at ch.shuttering.rapporte.Manager.SQLiteManager.SaveMangerSQLite.creatSQLiteSettings(SaveMangerSQLite.java:295)
    at ch.shuttering.rapporte.RapporteMain.create(RapporteMain.java:66)
    at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:311)
    at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1546)
    at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1262)

每次我从光标执行

getXXX()
查询时,都会收到错误,但如果我使用:

try {
    cursorSettings = dbHandler.rawQuery("SELECT * FROM "+TABLE_SETTINGS);
} catch (SQLiteGdxException e) {
    e.printStackTrace();
}
while (cursorSettings.next()) {
    Gdx.app.log("FromDb", String.valueOf(cursorSettings.getString(1)));
}

我得到了所有数据的正确输出。有什么问题吗?

sqlite libgdx cursor
1个回答
1
投票

这是Android类中的代码:

@Override
public long getLong (int columnIndex) {
    try {
        return cursor.getLong(columnIndex);
    } catch (SQLiteException e) {
        Gdx.app.log(DatabaseFactory.ERROR_TAG, "There was an error in getting the long", e);
        throw new SQLiteGdxRuntimeException(e);
    }
}

在Android中光标指向-1,所以需要先将其设置到正确的位置:

@Override
public long getLong (int columnIndex) {
    try {
        cursor.moveToFirst();
        return cursor.getLong(columnIndex);
    } catch (SQLiteException e) {
        Gdx.app.log(DatabaseFactory.ERROR_TAG, "There was an error in getting the long", e);
        throw new SQLiteGdxRuntimeException(e);
    }
}

您可以在类

AndroidCursor
的库gdx-sqlite-android.jar中找到此代码。

© www.soinside.com 2019 - 2024. All rights reserved.