我在Google Play市场上发布了一个应用程序。在其中我使用sqlasseThelper库进行数据库连接。实际上,我的应用程序是一个教程应用程序。其中还有一个表存储用户标记的喜欢的问题。现在我想更新问题表,但是我的问题是,如果我发布带有更新的问题表的新版本,那么最喜欢的问题的表将删除?正确的?那么,如何发布我的应用程序的新版本,该版本不会删除用户喜欢的表数据,而只会在该数据库中更新特定的表?我的问题有任何解决方案吗?
thanx提前。
您需要注意您的用户数据,并且SQliteOpenHelper
类中有一个onUpgrade()
,您可以在其中匹配database version
,如果您的数据库版本是更多,则来自以前的版本。在onUpgrade()
Fucntion中创建新表格,如果要在现有表中添加新数据,则您需要编写代码以添加新行或通过inert
或update
查询进行更新。另外,您可以通过alter
查询在现有表中添加新列。
请找到有关版本Upgarde的更多信息。
https://riggaroo.co.za/android-sqlite-database-use-onupgrade-correctly/
,如果您在资产中添加了数据库文件以获取初始数据,那么您需要更改数据库pragma
版本,而只是数据库版本,我们可以在SQliteOpenHelper
类中传递。
参见下面的onupgrade样本()
在我们的求职中,我们定义了以下内容:
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.e(TAG, "Updating table from " + oldVersion + " to " + newVersion);
//Added new column to book table - book rating
if (oldVersion < 2){
db.execSQL(DROP + BookEntry.TABLE_NAME);
db.execSQL(BookEntry.SQL_CREATE_BOOK_ENTRY_TABLE);
}
//Rename table to book_information - this is where things will start failing.
if (oldVersion < 3){
db.execSQL(DROP + BookEntry.TABLE_NAME);
db.execSQL(BookEntry.SQL_CREATE_BOOK_ENTRY_TABLE);
}
// Add new column for a calculated value. By this time, if I am upgrading from version 2 to
// version 4, my table would already contain the new column I am trying to add below,
// which would result in a SQLException. These situations are sometimes difficult to spot,
// as you basically need to test from every different version of database to upgrade from.
// Some upgrades might work and some might fail with this method.
// It is best to follow the other method that is on the master branch of this repo.
if (oldVersion < 4){
db.execSQL("ALTER TABLE " + BookEntry.TABLE_NAME + " ADD COLUMN calculated_pages_times_rating INTEGER;");
}
//As you can probably imagine, this is a terrible way to do upgrades, Please DONT DO IT!!!!
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.e(TAG, "Updating table from " + oldVersion + " to " + newVersion);
//Added new column to book table - book rating
if (oldVersion < 2){
db.execSQL(DROP + BookEntry.TABLE_NAME);
db.execSQL(BookEntry.SQL_CREATE_BOOK_ENTRY_TABLE);
}
//Rename table to book_information - this is where things will start failing.
if (oldVersion < 3){
db.execSQL(DROP + BookEntry.TABLE_NAME);
db.execSQL(BookEntry.SQL_CREATE_BOOK_ENTRY_TABLE);
}
// Add new column for a calculated value. By this time, if I am upgrading from version 2 to
// version 4, my table would already contain the new column I am trying to add below,
// which would result in a SQLException. These situations are sometimes difficult to spot,
// as you basically need to test from every different version of database to upgrade from.
// Some upgrades might work and some might fail with this method.
// It is best to follow the other method that is on the master branch of this repo.
if (oldVersion < 4){
db.execSQL("ALTER TABLE " + BookEntry.TABLE_NAME + " ADD COLUMN calculated_pages_times_rating INTEGER;");
}
//As you can probably imagine, this is a terrible way to do upgrades, Please DONT DO IT!!!!
}
当您在sqliteopenhelper类中更改数据库版本时 void onupgrade(sqlitedatabase db, int Oldversion, int newversion)将为拥有旧数据库版本的用户称呼。您应该在此处修改数据库,以在新的数据库版本中包括您的修改。如果您从此方法中删除Drop Table命令,则将不会删除现有用户数据