正如标题所说,我有一个安装了大约 1000 次的生产 Android 应用程序。我必须在 SQLite 中进行数据库更改,到目前为止,SQLite DB 的版本已设置为版本“1”。
希望我在评论中充分解释了下面的代码,此代码位于我的 SQLiteOpenHelper 类中,因此 onUpgrade 方法是该类的一部分:
// Provides an upgrade path for the DB when the apps version is updated.
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// First version of the DB was 1. Logic: each if statement will
// alter the DB cumulatively based on the version code. So, if the
// newVersion was version 3, there would be two if statements, one
// for oldVersion 1 and one for oldVersion 2. oldVersion 2 will
// contain the logic for upgrading from version 2 to 3, while
// oldVersion 1 will contain a combination of alter statements
// allowing the database to upgrade from version 1 directly to
// version 3.
if (oldVersion == 1) {
db.execSQL("ALTER TABLE plans ADD COLUMN " + App.CURRENCYCODE
+ " TEXT");
Locale locale = Locale.getDefault();
ContentValues content_values = new ContentValues();
content_values.put(App.CURRENCYCODE, locale.toString());
db.update(App.DBPLANS, content_values, App.ID + " > ?", new String[] {
"0"
});
}
if (oldVersion == 2) {
// Placeholder for next database upgrade instructions.
}
}
如果这里有任何陷阱,请告诉我。到目前为止,它的测试很好,尽管我非常担心会搞砸我的第一次数据库升级。我有大约 1,000 个用户,我不想失去所有用户。
再次感谢!