[英]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
.因此,除非
i
为0
,否则不应使用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.