public abstract class

SupportSQLiteOpenHelper.Callback

extends java.lang.Object

 java.lang.Object

↳androidx.sqlite.db.SupportSQLiteOpenHelper.Callback

Subclasses:

RoomOpenHelper

Overview

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

Summary

Fields
public final intversion

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

Constructors
publicCallback(int version)

Creates a new Callback to get database lifecycle events.

Methods
public voidonConfigure(SupportSQLiteDatabase db)

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

public voidonCorruption(SupportSQLiteDatabase db)

The method invoked when database corruption is detected.

public abstract voidonCreate(SupportSQLiteDatabase db)

Called when the database is created for the first time.

public voidonDowngrade(SupportSQLiteDatabase db, int oldVersion, int newVersion)

Called when the database needs to be downgraded.

public voidonOpen(SupportSQLiteDatabase db)

Called when the database has been opened.

public abstract voidonUpgrade(SupportSQLiteDatabase db, int oldVersion, int newVersion)

Called when the database needs to be upgraded.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Fields

public final int version

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

Constructors

public Callback(int version)

Creates a new Callback to get database lifecycle events.

Parameters:

version: The version for the database instance. See SupportSQLiteOpenHelper.Callback.version.

Methods

public 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 SupportSQLiteOpenHelper.Callback.onCreate(SupportSQLiteDatabase), SupportSQLiteOpenHelper.Callback.onUpgrade(SupportSQLiteDatabase, int, int), SupportSQLiteOpenHelper.Callback.onDowngrade(SupportSQLiteDatabase, int, int), or SupportSQLiteOpenHelper.Callback.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 SupportSQLiteDatabase.enableWriteAheadLogging() SupportSQLiteDatabase.setForeignKeyConstraintsEnabled(boolean), SupportSQLiteDatabase.setLocale(Locale), SupportSQLiteDatabase.setMaximumSize(long), or executing PRAGMA statements.

Parameters:

db: The database.

public abstract 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: The database.

public abstract 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: The database.
oldVersion: The old database version.
newVersion: The new database version.

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

Called when the database needs to be downgraded. This is strictly similar to SupportSQLiteOpenHelper.Callback.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: The database.
oldVersion: The old database version.
newVersion: The new database version.

public void onOpen(SupportSQLiteDatabase db)

Called when the database has been opened. The implementation should check SupportSQLiteDatabase.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 SupportSQLiteOpenHelper.Callback.onConfigure(SupportSQLiteDatabase) instead.

Parameters:

db: The database.

public void onCorruption(SupportSQLiteDatabase db)

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

Parameters:

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