Android SQLite database

SQLite is a small relational database built into Android. Is a text-based database. Android provides full support for SQLite databases. Any class in an application can access any database by name, but not any class outside the application.

In Android, SQLiteOpenHelper is used to operate SQLite database.

SQLiteOpenHelper

SQLiteOpenHelper encapsulates SQLite database operations, including connecting to the database, opening the database, upgrading and demoting the database version, adding, deleting, searching, and changing the database.

SQLiteOpenHelper ();

OnCreate () creates the database

If the database file does not exist, only onCreate() is called (this method is called once when the database is created).

In the call SQLiteOpenHelper. GetWritableDatabase (), SQLiteOpenHelper first access to the current version of the database.

The onCreate method is executed when the version number is 0 (it was 0 when the database file was first created).

If the version number is not 0, compare with the latest version number. If the version number is greater than, perform onUpgrade; otherwise, perform onDowngrade.

However, the reversion method is very simple to implement: directly throw an exception, that is, the database does not allow the reversion operation, which is also normal.

2. OnUpgrade () Upgrades the database

If the database file exists and the current version is higher than the last one created or upgraded, SQLiteOpenHelper calls the onUpgrade() method, which updates the database version.

3. close()

Close all open database objects

4. getWritableDatabase()

Create or open a database that can read/write

5. getReadableDatabase()

Create or open a readable database