【发布时间】:2015-06-02 14:02:56
【问题描述】:
我在我的应用程序中使用 SQLiteDatabase。我的 SQLiteOpenHelper.onUpgrade(..) 方法如下所示:
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//Save current data
ArrayList<Course> tempCourses = getAllCourses();
//Drop tables
db.execSQL("DROP TABLE IF EXISTS " + TABLE_COURSES);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ASSIGNMENTS);
//create new tables
onCreate(db);
//Add temp data back to the database
for (Course course : tempCourses) {
addCourse(course);
}
}
我想在升级数据库架构时保留旧的用户数据。 但是,当我增加数据库版本并启动我的应用程序时,出现以下错误:
...
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tak3r07.unihelper/com.tak3r07.CourseStatistics.MainActivity}: java.lang.IllegalStateException: getDatabase called recursively
...
这是因为 getAllCourses() 将再次打开数据库以获取调用 onUpgrade 方法的数据(明显循环)。 但是我应该如何存储我的用户数据呢?
问候, tak3r07
【问题讨论】:
-
你到底想做什么?添加一列?