Skip to content

Most visited

Recently visited

navigation
added in version 1.0.0
belongs to Maven artifact android.arch.persistence:db:1.0.0

SupportSQLiteOpenHelper.Callback

public static abstract class SupportSQLiteOpenHelper.Callback
extends Object

java.lang.Object
   ↳ android.arch.persistence.db.SupportSQLiteOpenHelper.Callback


Handles various lifecycle events for the SQLite connection, similar to SQLiteOpenHelper.

Summary

Fields

public final int version

Version number of the database (starting at 1); if the database is older, onUpgrade(SupportSQLiteDatabase, int, int) will be used to upgrade the database; if the database is newer, onDowngrade(SupportSQLiteDatabase, int, int) will be used to downgrade the database.

Public constructors

SupportSQLiteOpenHelper.Callback(int version)

Creates a new Callback to get database lifecycle events.

Public methods

void onConfigure(SupportSQLiteDatabase db)

Called when the database connection is being configured, to enable features such as write-ahead logging or foreign key support.

void onCorruption(SupportSQLiteDatabase db)

The method invoked when database corruption is detected.

abstract void onCreate(SupportSQLiteDatabase db)

Called when the database is created for the first time.

void onDowngrade(SupportSQLiteDatabase db, int oldVersion, int newVersion)

Called when the database needs to be downgraded.

void onOpen(SupportSQLiteDatabase db)

Called when the database has been opened.

abstract void onUpgrade(SupportSQLiteDatabase db, int oldVersion, int newVersion)

Called when the database needs to be upgraded.

Inherited methods

From class java.lang.Object

Fields

version

added in version 1.0.0
int version

Version number of the database (starting at 1); if the database is older, onUpgrade(SupportSQLiteDatabase, int, int) will be used to upgrade the database; if the database is newer, onDowngrade(SupportSQLiteDatabase, int, int) will be used to downgrade the database.

Public constructors

SupportSQLiteOpenHelper.Callback

added in version 1.0.0
SupportSQLiteOpenHelper.Callback (int version)

Creates a new Callback to get database lifecycle events.

Parameters
version int: The version for the database instance. See version.

Public methods

onConfigure

added in version 1.0.0
void onConfigure (SupportSQLiteDatabase db)

Called when the database connection is being configured, to enable features such as write-ahead logging or foreign key support.

This method is called before onCreate(SupportSQLiteDatabase), onUpgrade(SupportSQLiteDatabase, int, int), onDowngrade(SupportSQLiteDatabase, int, int), or onOpen(SupportSQLiteDatabase) are called. It should not modify the database except to configure the database connection as required.

This method should only call methods that configure the parameters of the database connection, such as enableWriteAheadLogging() setForeignKeyConstraintsEnabled(boolean), setLocale(Locale), setMaximumSize(long), or executing PRAGMA statements.

Parameters
db SupportSQLiteDatabase: The database.

onCorruption

added in version 1.0.0
void onCorruption (SupportSQLiteDatabase db)

The method invoked when database corruption is detected. Default implementation will delete the database file.

Parameters
db SupportSQLiteDatabase: the SupportSQLiteDatabase object representing the database on which corruption is detected.

onCreate

added in version 1.0.0
void onCreate (SupportSQLiteDatabase db)

Called when the database is created for the first time. This is where the creation of tables and the initial population of the tables should happen.

Parameters
db SupportSQLiteDatabase: The database.

onDowngrade

added in version 1.0.0
void onDowngrade (SupportSQLiteDatabase db, 
                int oldVersion, 
                int newVersion)

Called when the database needs to be downgraded. This is strictly similar to onUpgrade(SupportSQLiteDatabase, int, int) method, but is called whenever current version is newer than requested one. However, this method is not abstract, so it is not mandatory for a customer to implement it. If not overridden, default implementation will reject downgrade and throws SQLiteException

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

Parameters
db SupportSQLiteDatabase: The database.

oldVersion int: The old database version.

newVersion int: The new database version.

onOpen

added in version 1.0.0
void onOpen (SupportSQLiteDatabase db)

Called when the database has been opened. The implementation should check isReadOnly() before updating the database.

This method is called after the database connection has been configured and after the database schema has been created, upgraded or downgraded as necessary. If the database connection must be configured in some way before the schema is created, upgraded, or downgraded, do it in onConfigure(SupportSQLiteDatabase) instead.

Parameters
db SupportSQLiteDatabase: The database.

onUpgrade

added in version 1.0.0
void onUpgrade (SupportSQLiteDatabase db, 
                int oldVersion, 
                int newVersion)

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.

Parameters
db SupportSQLiteDatabase: The database.

oldVersion int: The old database version.

newVersion int: The new database version.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)