Вы должны заботиться о ваших пользовательских данных и есть onUpgrade()
находится там в SQliteOpenHelper
классе, в котором вы можете соответствовать вашим database version
и если вашей версии базы данных более чем из предыдущей версии затем создать новые таблицы в onUpgrade()
fucntion, И если вы хотите добавить новые данные в существующую таблицу, тогда вам нужно написать код для добавления новых строк или обновления через inert
или update
. Также вы можете добавить новые столбцы в существующие таблицы через запрос alter
.
более подробную информацию о версии upgradearde.
https://riggaroo.co.za/android-sqlite-database-use-onupgrade-correctly/
И если вы добавили свой файл базы данных активов для исходных данных Затем необходимо изменить базу данных pragma
версия то будет не что иное, версия базы данных, которые мы можем передать в SQliteOpenHelper
классе.
смотри ниже образец onUpgrade()
В нашем 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!!!!
}
Да. Это приведет к удалению ваших данных. Вы должны выполнить операцию в методе 'onUpgrade()'. В которой вы должны реализовать таблицу «ALTER». – Piyush