简体   繁体   中英

Error with reading data from SQLite database Android Studio

I'm trying to read data from my SQLite Database I created in android studio. I keep receiving the error "Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0". I assume it has to do with my ReadData Function but I'm not sure what needs to be fixed. I'm trying to read data fro a specific row of a specific column to a variable in another activity. The database is connected but the data I'm trying to pass through to the other activity is giving me an error. If more explanation is needed please let me know.

---------THIS IS THE LINE IN MY ACTIVITY THAT GIVING ME THE ERROR IN LINE 980 OF BallProcessDataActivity

distance = databaseHelper.ReadData(i,"distance").getFloat(i);

--------------IN MY DATABASE HELPER JAVA CLASS---------------------

public Cursor ReadData(int rowNum, String columnName){
        String query = "SELECT '" + columnName + "'"+ " FROM " + TABLE_NAME + " WHERE ID = '" + rowNum + "';";

        SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();

        Cursor res = sqLiteDatabase.rawQuery(query, null);

        res.close();
        return res;


    }

----------------------HERE IS THE ERROR I RECEIVE-----------------

2020-07-30 12:51:27.238 17801-17801/wtzn.wtbtble901 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: wtzn.wtbtble901, PID: 17801
    java.lang.RuntimeException: Unable to start activity ComponentInfo{wtzn.wtbtble901/wtzn.wtbtble901.BallProcessDataActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
        at android.database.AbstractCursor.checkPosition(AbstractCursor.java:515)
        at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:138)
        at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:76)
        at wtzn.wtbtble901.BallProcessDataActivity.ShotAnalyze(BallProcessDataActivity.java:980)
        at wtzn.wtbtble901.BallProcessDataActivity.onCreate(BallProcessDataActivity.java:969)
        at android.app.Activity.performCreate(Activity.java:7825)
        at android.app.Activity.performCreate(Activity.java:7814)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 

You can check the number or rows in the result set

Cursor res = sqLiteDatabase.rawQuery(query, null);
if (res.getCount() > 0) {
   res.moveToPosition(rowNum);
} else {
   // error
}

Assuming that ID is the primary key of your table, your query (if it was syntactically correct) would return only 1 row with only 1 column.
So you shouldn't be using i as a parameter to getFloat(i) unless i is 0 , because the only column's index is 0 .
Also you should not close the Cursor that your method returns.
You can close it after you call the method and use it, when it is no longer needed. In your query you must not concatenate single quotes around the column's name and use a ? placeholder for the parameter rowNum :

public Cursor ReadData(int rowNum, String columnName){
    String query = "SELECT " + columnName + " FROM " + TABLE_NAME + " WHERE ID = ?";
    SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
    Cursor res = sqLiteDatabase.rawQuery(query, new String[] {String.valueOf(rowNum)});
    return res;
}

Now call the method ReadData() like this:

Cursor c = databaseHelper.ReadData(i, "distance");
if (c.moveToFirst()) distance = c.getFloat(0);
c.close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM