问题描述
在我的代码中,我正在创建一个sqlite数据库和一个表,并将数据添加到表中,这部分工作正常。但是,当我访问数据时会出现问题。它检索存储的数据,最后使应用程序崩溃。 while循环会导致这种情况吗?
这是我的代码:
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();
}
}
}
以下是日志:
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)
解决方法
在游标的各行之间循环的方式是错误的。
删除对c.moveToFirst();
的第一个调用,然后使用c.moveToNext()
来检查游标中是否还有另一行:
while (c.moveToNext()) {
Log.i("name",c.getString(nameIndex));
Log.i("age",c.getString(ageIndex));
}
这样,循环将在访问最后一行之后停止,因为c.moveToNext()
将返回false
。