字段槽的错误请求,SQLite - Android

时间:2011-12-19 20:50:12

标签: android sqlite

我有一个包含6列的表,我正在尝试从3列中获取所有行并将其存储在数组中以使用它。我的查询如下:

public Cursor fetchInfo(long rowId) throws SQLException
    {
        Cursor mCursor = mDb.query(true, DATABASE_TABLE, new String[] {"_id", "xCoord", "yCoord", "color"}, "_id" + "=" + rowId, null, null, null, null, null);

        if(mCursor != null)
        {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

然后我这样称呼它:

private void getInfo()
    {
        Cursor mCursor = mDbHelper.fetchInfo(mRowId);
        x = new float[mCursor.getCount()];
        y = new float[mCursor.getCount()];
        color = new int[mCursor.getCount()];

        if(mCursor.moveToFirst())
        {
            for(int i = 0; i < mCursor.getCount(); i++)
            {
                x[i] = mCursor.getFloat(3);
                y[i] = mCursor.getFloat(4); //Line 76, this is where the error is occurs.
                color[i] = mCursor.getInt(5);
            }
        }
        mCursor.close();
    }

但是,当我尝试运行时,我收到一条错误消息:

12-19 15:40:36.903: E/CursorWindow(276): Bad request for field slot 0,4. numRows = 1, numColumns = 4
12-19 15:40:36.903: D/AndroidRuntime(276): Shutting down VM
12-19 15:40:36.912: W/dalvikvm(276): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
12-19 15:40:36.922: E/AndroidRuntime(276): FATAL EXCEPTION: main
12-19 15:40:36.922: E/AndroidRuntime(276): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.x17.projects.strikezone/com.x17.projects.strikezone.SavedEntries}: java.lang.IllegalStateException: get field slot from row 0 col 4 failed
12-19 15:40:36.922: E/AndroidRuntime(276):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
12-19 15:40:36.922: E/AndroidRuntime(276):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-19 15:40:36.922: E/AndroidRuntime(276):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-19 15:40:36.922: E/AndroidRuntime(276):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-19 15:40:36.922: E/AndroidRuntime(276):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-19 15:40:36.922: E/AndroidRuntime(276):  at android.os.Looper.loop(Looper.java:123)
12-19 15:40:36.922: E/AndroidRuntime(276):  at android.app.ActivityThread.main(ActivityThread.java:4627)
12-19 15:40:36.922: E/AndroidRuntime(276):  at java.lang.reflect.Method.invokeNative(Native Method)
12-19 15:40:36.922: E/AndroidRuntime(276):  at java.lang.reflect.Method.invoke(Method.java:521)
12-19 15:40:36.922: E/AndroidRuntime(276):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-19 15:40:36.922: E/AndroidRuntime(276):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-19 15:40:36.922: E/AndroidRuntime(276):  at dalvik.system.NativeStart.main(Native Method)
12-19 15:40:36.922: E/AndroidRuntime(276): Caused by: java.lang.IllegalStateException: get field slot from row 0 col 4 failed
12-19 15:40:36.922: E/AndroidRuntime(276):  at android.database.CursorWindow.getDouble_native(Native Method)
12-19 15:40:36.922: E/AndroidRuntime(276):  at android.database.CursorWindow.getFloat(CursorWindow.java:451)
12-19 15:40:36.922: E/AndroidRuntime(276):  at android.database.AbstractWindowedCursor.getFloat(AbstractWindowedCursor.java:123)
12-19 15:40:36.922: E/AndroidRuntime(276):  at com.x17.projects.strikezone.SavedEntries.getInfo(SavedEntries.java:76)
12-19 15:40:36.922: E/AndroidRuntime(276):  at com.x17.projects.strikezone.SavedEntries.onCreate(SavedEntries.java:46)
12-19 15:40:36.922: E/AndroidRuntime(276):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-19 15:40:36.922: E/AndroidRuntime(276):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
12-19 15:40:36.922: E/AndroidRuntime(276):  ... 11 more

我是数据库的新手,所以我不知道如何解决这个问题。我已经尝试了一些不同的方法来解决这个问题,但没有成功。我在这做错了什么?我应该这样做吗?

1 个答案:

答案 0 :(得分:4)

fetchInfo()方法中的查询中,您从DB Table获得了4列。

正如文档所述getFloat(int index),此方法需要索引:

  

index - 目标列的从零开始的索引。

所以你应该按如下方式使用索引:

0 - _id

1 - 代表xCoord

2 - yCoord

3 - 代表color