简体   繁体   English

从 SQLite 数据库访问数据后应用程序崩溃 android

[英]App crashes after accessing data from SQLite Database android

In my code, I am creating a SQLite database and a table, and adding the data into the table, this part is working fine.在我的代码中,我正在创建一个 SQLite 数据库和一个表,并将数据添加到表中,这部分工作正常。 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? while循环会导致这个吗?

Here's my code:这是我的代码:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //creating  a new Or accessing an existing database
        SQLiteDatabase myDatabase = this.openOrCreateDatabase("Users",MODE_PRIVATE,null);

        //creating a table if it does not exist
        myDatabase.execSQL("CREATE TABLE IF NOT EXISTS users(name VARCHAR,age INT(3))");

        //inserting data into the users table
        //single row of values
        myDatabase.execSQL("INSERT INTO users(name,age) VALUES('Robert',28)");
        //multiple rows of values
        myDatabase.execSQL("INSERT INTO users(name,age) VALUES('Derek',30), ('Israel',31), ('Jared',24)");

        //accessing data and traversing across table users
        Cursor c = myDatabase.rawQuery("SELECT * FROM users",null);

        int nameIndex = c.getColumnIndex("name");   //setting index  for name column
        int ageIndex = c.getColumnIndex("age");     //setting index  for age column

        c.moveToFirst(); //setting the cursor on starting position

//traversing across the table row by row
        while (c != null){
            Log.i("name",c.getString(nameIndex));
            Log.i("age",c.getString(ageIndex));

            c.moveToNext();
        }

    }
} 

Here are the logs:这是日志:

2020-10-26 13:29:53.199 18545-18545/com.example.databasedemo1 I/name: Robert
2020-10-26 13:29:53.199 18545-18545/com.example.databasedemo1 I/age: 28
2020-10-26 13:29:53.199 18545-18545/com.example.databasedemo1 I/name: Derek
2020-10-26 13:29:53.199 18545-18545/com.example.databasedemo1 I/age: 30
2020-10-26 13:29:53.199 18545-18545/com.example.databasedemo1 I/name: Israel
2020-10-26 13:29:53.199 18545-18545/com.example.databasedemo1 I/age: 31
2020-10-26 13:29:53.199 18545-18545/com.example.databasedemo1 I/name: Jared
2020-10-26 13:29:53.199 18545-18545/com.example.databasedemo1 I/age: 24
2020-10-26 13:29:53.200 18545-18545/com.example.databasedemo1 D/AndroidRuntime: Shutting down VM
2020-10-26 13:29:53.206 18545-18545/com.example.databasedemo1 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.databasedemo1, PID: 18545
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.databasedemo1/com.example.databasedemo1.MainActivity}: 

android.database.CursorIndexOutOfBoundsException: Index 4 requested, with a size of 4
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
     Caused by: android.database.CursorIndexOutOfBoundsException: Index 4 requested, with a size of 4
        at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
        at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
        at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
        at com.example.databasedemo1.MainActivity.onCreate(MainActivity.java:39)
        at android.app.Activity.performCreate(Activity.java:6975)

The way that you loop through the rows of the Cursor is wrong.您循环遍历 Cursor 行的方式是错误的。
Remove the 1st call to c.moveToFirst();删除对c.moveToFirst(); and use c.moveToNext() to check if there is another row in the Cursor:并使用c.moveToNext()检查 Cursor 中是否还有另一行:

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

This way the loop will stop after the last row is accessed because c.moveToNext() will return false .这样循环将在访问最后一行后停止,因为c.moveToNext()将返回false

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

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