简体   繁体   English

从 SQLite 数据库 Android Studio 读取数据时出错

[英]Error with reading data from SQLite database Android Studio

I'm trying to read data from my SQLite Database I created in android studio.我正在尝试从我在 android 工作室创建的 SQLite 数据库中读取数据。 I keep receiving the error "Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0".我不断收到错误“原因:android.database.CursorIndexOutOfBoundsException:请求索引 0,大小为 0”。 I assume it has to do with my ReadData Function but I'm not sure what needs to be fixed.我认为这与我的 ReadData Function 有关,但我不确定需要修复什么。 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 ---------这是我的活动中的行,它在 BallProcessDataActivity 的第 980 行中给了我错误

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

--------------IN MY DATABASE HELPER JAVA CLASS--------------------- --------------在我的数据库助手 JAVA 类中---------

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.假设ID是您的表的主键,您的查询(如果它在语法上是正确的)将只返回 1 行和 1 列。
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 .因此,除非i0 ,否则不应使用i作为getFloat(i)的参数,因为唯一列的索引是0
Also you should not close the Cursor that your method returns.此外,您不应关闭您的方法返回的Cursor
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 :参数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:现在像这样调用方法ReadData()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 SQLite 数据库读取数据到新的 Activity - Android Studio - Reading Data from SQLite Database to a new Activity - Android Studio Android Studio 项目无法从 SQLite 数据库中正确读取? - Android Studio project not reading correctly from SQLite database? Cursor 尝试从 SQLite 数据库(Android Studio)读取数据时出错 - Cursor running into error while attempting to read data from SQLite database (Android Studio) 如何从android studio中的recycler视图将数据添加到SQLite数据库中 - How to add data into SQLite database from recycler view in android studio Android Studio-从SQLite数据库检索数据并插入Spinner - Android Studio - Retrieve Data From SQLite Database and insert into Spinner Android Studio-无法从预安装的sqlite数据库检索数据 - Android Studio - unable to retreive data from preinstalled sqlite database 从Android Studio中的SQLite数据库中以字符串形式检索数据 - Retrieve Data As String From SQLite Database in Android Studio Android Studio,如何从Sqlite数据库中检索数据并将其显示到textview中? - Android Studio, how to retrieve data from Sqlite database and display it into textview? Android Studio 未从 Firebase 实时数据库读取检索到的数据 - Android Studio is not reading the retreived data from Firebase realtime database 在Android Studio中从SQlite提取数据时出错 - Error while fetching data from SQlite in Android Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM