Get the App
SLTechnology News&Howtos  ›  Database  › 

The creation of Andorid Sqlite Database

Shulou Source: shulou.com Published: 2022-06-01 02:36:33 10月15日 Update

The creation of Andorid Sqlite Database

To create a database, you will use an abstract class of SqliteOpenHelper. To create a database using this class, you must inherit the abstract class and use its methods.

The specific steps are as follows:

1. The custom class inherits SqliteOpenHelper

2. Add a constructor, in which the method of the parent class will be called to pass parameters. The name and meaning of the parameters are as follows

Public BookStoreDbOpenHelper (Context context) {

Super (context, "bookstore.db", null, 1)

/ * *

* parameters:

* 1 dint context context object

* 2 name the name of the database to be created

* 3 cursor factory, which is used to receive data

* 4 version number of the database to be created

, /

}

3, override the method of the parent class

Two important methods in the parent class, onCreate (SQLiteDatabase db) and onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion)

The oncreate () method is called back when the database is created for the first time, which is suitable for initializing the table structure, that is, creating the required database tables.

OnUpdate () method: this method is called back when the database is updated, that is, when the version number changes, and is suitable for table operations in this method.

4, create a SqliteOpenHelper subclass object

/ / create a custom SqliteOpenHelper subclass to create the database

BookStoreDbOpenHelper helper = new BookStoreDbOpenHelper (mContext)

/ / call the parent method of this class to create the database

SQLiteDatabase db = helper.getReadableDatabase (); / / get a readable database object

The database is created and the database file exists in the data/data/ package name / databases directory

Tags: Data database method parameter object name subclass version constructor important upper and lower context two that is meaning factory file step cursor directory Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Huawei Shulou Information Shulou Tech Info Redmi