Compartir a través de


SQLiteOpenHelper.OnUpgrade(SQLiteDatabase, Int32, Int32) Method

Definition

Called when the database needs to be upgraded.

[Android.Runtime.Register("onUpgrade", "(Landroid/database/sqlite/SQLiteDatabase;II)V", "GetOnUpgrade_Landroid_database_sqlite_SQLiteDatabase_IIHandler")]
public abstract void OnUpgrade (Android.Database.Sqlite.SQLiteDatabase? db, int oldVersion, int newVersion);
[<Android.Runtime.Register("onUpgrade", "(Landroid/database/sqlite/SQLiteDatabase;II)V", "GetOnUpgrade_Landroid_database_sqlite_SQLiteDatabase_IIHandler")>]
abstract member OnUpgrade : Android.Database.Sqlite.SQLiteDatabase * int * int -> unit

Parameters

db
SQLiteDatabase

The database.

oldVersion
Int32

The old database version.

newVersion
Int32

The new database version.

Attributes

Remarks

Called when the database needs to be upgraded. The implementation should use this method to drop tables, add tables, or do anything else it needs to upgrade to the new schema version.

The SQLite ALTER TABLE documentation can be found here. If you add new columns you can use ALTER TABLE to insert them into a live table. If you rename or remove columns you can use ALTER TABLE to rename the old table, then create the new table and then populate the new table with the contents of the old table.

This method executes within a transaction. If an exception is thrown, all changes will automatically be rolled back.

<em>Important:</em> You should NOT modify an existing migration step from version X to X+1 once a build has been released containing that migration step. If a migration step has an error and it runs on a device, the step will NOT re-run itself in the future if a fix is made to the migration step.

For example, suppose a migration step renames a database column from foo to bar when the name should have been baz. If that migration step is released in a build and runs on a user's device, the column will be renamed to bar. If the developer subsequently edits this same migration step to change the name to baz as intended, the user devices which have already run this step will still have the name bar. Instead, a NEW migration step should be created to correct the error and rename bar to baz, ensuring the error is corrected on devices which have already run the migration step with the error.

Java documentation for android.database.sqlite.SQLiteOpenHelper.onUpgrade(android.database.sqlite.SQLiteDatabase, int, int).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to