onUpgrade method
suggest changeSQLiteOpenHelper
is a helper class to manage database creation and version management.
In this class, the onUpgrade()
method is responsible for upgrading the database when you make changes to the schema. It is called when the database file already exists, but its version is lower than the one specified in the current version of the app. For each database version, the specific changes you made have to be applied.
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Loop through each version when an upgrade occurs.
for (int version = oldVersion + 1; version <= newVersion; version++) {
switch (version) {
case 2:
// Apply changes made in version 2
db.execSQL(
"ALTER TABLE " +
TABLE_PRODUCTS +
" ADD COLUMN " +
COLUMN_DESCRIPTION +
" TEXT;"
);
break;
case 3:
// Apply changes made in version 3
db.execSQL(CREATE_TABLE_TRANSACTION);
break;
}
}
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents