应用程序在从SQLite数据库访问数据后崩溃,安卓。

huangapple go评论123阅读模式
英文:

App crashes after accessing data from SQLite Database android

问题

在我的代码中,我正在创建一个SQLite数据库和一个表,并将数据添加到表中,这部分工作正常。但是问题出现在访问数据时。它检索存储的数据,最后导致应用程序崩溃。是 while 循环引起了这个问题吗?

以下是我的代码:

  1. public class MainActivity extends AppCompatActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. // 创建一个新的数据库或访问现有数据库
  7. SQLiteDatabase myDatabase = this.openOrCreateDatabase("Users", MODE_PRIVATE, null);
  8. // 如果表不存在,则创建一个表
  9. myDatabase.execSQL("CREATE TABLE IF NOT EXISTS users(name VARCHAR,age INT(3))");
  10. // 将数据插入到用户表中
  11. // 单行值
  12. myDatabase.execSQL("INSERT INTO users(name,age) VALUES('Robert',28)");
  13. // 多行值
  14. myDatabase.execSQL("INSERT INTO users(name,age) VALUES('Derek',30), ('Israel',31), ('Jared',24)");
  15. // 访问数据并遍历用户表
  16. Cursor c = myDatabase.rawQuery("SELECT * FROM users", null);
  17. int nameIndex = c.getColumnIndex("name"); // 为姓名列设置索引
  18. int ageIndex = c.getColumnIndex("age"); // 为年龄列设置索引
  19. c.moveToFirst(); // 将光标设置在起始位置
  20. // 逐行遍历表格
  21. while (c != null){
  22. Log.i("name", c.getString(nameIndex));
  23. Log.i("age", c.getString(ageIndex));
  24. c.moveToNext();
  25. }
  26. }
  27. }

以下是日志:

  1. 2020-10-26 13:29:53.199 18545-18545/com.example.databasedemo1 I/name: Robert
  2. 2020-10-26 13:29:53.199 18545-18545/com.example.databasedemo1 I/age: 28
  3. 2020-10-26 13:29:53.199 18545-18545/com.example.databasedemo1 I/name: Derek
  4. 2020-10-26 13:29:53.199 18545-18545/com.example.databasedemo1 I/age: 30
  5. 2020-10-26 13:29:53.199 18545-18545/com.example.databasedemo1 I/name: Israel
  6. 2020-10-26 13:29:53.199 18545-18545/com.example.databasedemo1 I/age: 31
  7. 2020-10-26 13:29:53.199 18545-18545/com.example.databasedemo1 I/name: Jared
  8. 2020-10-26 13:29:53.199 18545-18545/com.example.databasedemo1 I/age: 24
  9. 2020-10-26 13:29:53.200 18545-18545/com.example.databasedemo1 D/AndroidRuntime: Shutting down VM
  10. 2020-10-26 13:29:53.206 18545-18545/com.example.databasedemo1 E/AndroidRuntime: FATAL EXCEPTION: main
  11. Process: com.example.databasedemo1, PID: 18545
  12. java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.databasedemo1/com.example.databasedemo1.MainActivity}:
  13. android.database.CursorIndexOutOfBoundsException: Index 4 requested, with a size of 4
  14. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
  15. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
  16. at android.app.ActivityThread.-wrap11(Unknown Source:0)
  17. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
  18. at android.os.Handler.dispatchMessage(Handler.java:105)
  19. at android.os.Looper.loop(Looper.java:164)
  20. at android.app.ActivityThread.main(ActivityThread.java:6541)
  21. at java.lang.reflect.Method.invoke(Native Method)
  22. at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
  23. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
  24. Caused by: android.database.CursorIndexOutOfBoundsException: Index 4 requested, with a size of 4
  25. at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
  26. at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
  27. at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
  28. at com.example.databasedemo1.MainActivity.onCreate(MainActivity.java:39)
  29. at android.app.Activity.performCreate(Activity.java:6975)
英文:

In my code, I am creating a SQLite database and a table, and adding the data into the table, this part is working fine. But the problem arises when I'm accessing the data. It retrieves the stored data and at the end crashes the app. Does the while loop cause this?

Here's my code:

  1. public class MainActivity extends AppCompatActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. //creating a new Or accessing an existing database
  7. SQLiteDatabase myDatabase = this.openOrCreateDatabase("Users",MODE_PRIVATE,null);
  8. //creating a table if it does not exist
  9. myDatabase.execSQL("CREATE TABLE IF NOT EXISTS users(name VARCHAR,age INT(3))");
  10. //inserting data into the users table
  11. //single row of values
  12. myDatabase.execSQL("INSERT INTO users(name,age) VALUES('Robert',28)");
  13. //multiple rows of values
  14. myDatabase.execSQL("INSERT INTO users(name,age) VALUES('Derek',30), ('Israel',31), ('Jared',24)");
  15. //accessing data and traversing across table users
  16. Cursor c = myDatabase.rawQuery("SELECT * FROM users",null);
  17. int nameIndex = c.getColumnIndex("name"); //setting index for name column
  18. int ageIndex = c.getColumnIndex("age"); //setting index for age column
  19. c.moveToFirst(); //setting the cursor on starting position
  20. //traversing across the table row by row
  21. while (c != null){
  22. Log.i("name",c.getString(nameIndex));
  23. Log.i("age",c.getString(ageIndex));
  24. c.moveToNext();
  25. }
  26. }
  27. }

Here are the logs:

  1. 2020-10-26 13:29:53.199 18545-18545/com.example.databasedemo1 I/name: Robert
  2. 2020-10-26 13:29:53.199 18545-18545/com.example.databasedemo1 I/age: 28
  3. 2020-10-26 13:29:53.199 18545-18545/com.example.databasedemo1 I/name: Derek
  4. 2020-10-26 13:29:53.199 18545-18545/com.example.databasedemo1 I/age: 30
  5. 2020-10-26 13:29:53.199 18545-18545/com.example.databasedemo1 I/name: Israel
  6. 2020-10-26 13:29:53.199 18545-18545/com.example.databasedemo1 I/age: 31
  7. 2020-10-26 13:29:53.199 18545-18545/com.example.databasedemo1 I/name: Jared
  8. 2020-10-26 13:29:53.199 18545-18545/com.example.databasedemo1 I/age: 24
  9. 2020-10-26 13:29:53.200 18545-18545/com.example.databasedemo1 D/AndroidRuntime: Shutting down VM
  10. 2020-10-26 13:29:53.206 18545-18545/com.example.databasedemo1 E/AndroidRuntime: FATAL EXCEPTION: main
  11. Process: com.example.databasedemo1, PID: 18545
  12. java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.databasedemo1/com.example.databasedemo1.MainActivity}:
  13. android.database.CursorIndexOutOfBoundsException: Index 4 requested, with a size of 4
  14. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
  15. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
  16. at android.app.ActivityThread.-wrap11(Unknown Source:0)
  17. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
  18. at android.os.Handler.dispatchMessage(Handler.java:105)
  19. at android.os.Looper.loop(Looper.java:164)
  20. at android.app.ActivityThread.main(ActivityThread.java:6541)
  21. at java.lang.reflect.Method.invoke(Native Method)
  22. at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
  23. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
  24. Caused by: android.database.CursorIndexOutOfBoundsException: Index 4 requested, with a size of 4
  25. at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
  26. at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
  27. at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
  28. at com.example.databasedemo1.MainActivity.onCreate(MainActivity.java:39)
  29. at android.app.Activity.performCreate(Activity.java:6975)

答案1

得分: 1

循环遍历 Cursor 行的方式是错误的。<br/> 删除第一次调用的 c.moveToFirst();,并使用 c.moveToNext() 来检查 Cursor 中是否还有另一行:

  1. while (c.moveToNext()) {
  2. Log.i("name", c.getString(nameIndex));
  3. Log.i("age", c.getString(ageIndex));
  4. }

这样,循环将在访问最后一行后停止,因为 c.moveToNext() 将返回 false

英文:

The way that you loop through the rows of the Cursor is wrong.<br/>
Remove the 1st call to c.moveToFirst(); and use c.moveToNext() to check if there is another row in the Cursor:

  1. while (c.moveToNext()) {
  2. Log.i(&quot;name&quot;,c.getString(nameIndex));
  3. Log.i(&quot;age&quot;,c.getString(ageIndex));
  4. }

This way the loop will stop after the last row is accessed because c.moveToNext() will return false.

huangapple
  • 本文由 发表于 2020年10月26日 17:04:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/64534124.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定