Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bad request for field slot, SQLite - Android

Tags:

android

sqlite

I have a table with 6 columns in it, I'm trying to fetch all rows from only 3 of the columns and store it in an array to use it. My query looks like this:

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

then I'm calling it like so:

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

However, when I try to run this, I get an error saying:

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

I am new to databases, so I'm not sure how to fix this. I've tried some different ways to fix this but with no success. What am I doing wrong here? Should I be doing this differently?

like image 933
user990230 Avatar asked Mar 04 '26 02:03

user990230


1 Answers

In your query inside the fetchInfo() method you are getting 4 columns from your DB Table.

As the documentation states getFloat(int index) this method expects as index:

index - the zero-based index of the target column.

So you should use indexes as follows:

0 - for _id

1 - for xCoord

2 - for yCoord

3 - for color

like image 181
Ovidiu Latcu Avatar answered Mar 05 '26 15:03

Ovidiu Latcu