The databases used under Android are:SQLite, embedded lightweight
To create a SQLite database is generally to inherit the Sqliteopenhelper class.
The official documentation is this:
Since it is an abstract class that must be inherited with a class, there are two abstract methods OnCreate and Onupgrade must be implemented.
OnCreate method : is called when the database is first created (but is not called when created), especially for table structure initialization, requires SQL statement
Onupgrade Method : Database version number changes will be executed, especially for the table structure changes, after Android4.0 version can only increase or decrease, or there will be an exception
The following creates a Mysqliteopenhelper class to inherit from the Sqliteopenhelper class.
1 Public classMysqliteopenhelperextendsSqliteopenhelper {2 3 PublicMysqliteopenhelper (Context context) {4 //Context: Contexts Name: Database file names Factory: Used to create cursor objects, default NULL5 //version: The release number of the database, starting with 1, if a change occurs, the Onupgrade method calls6 Super(Context, "starry.db",NULL, 2);7 }8 @Override9 Public voidonCreate (Sqlitedatabase db) {Ten OneDb.execsql ("CREATE table info (_id integer primary key autoincrement, name varchar (20))"); A } - @Override - Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) { theDb.execsql ("ALTER TABLE info add phone varchar (11)"); - } - -}View Code
When you instantiate a mysqliteopenhelper in Mainactivity.java, you can initialize the database by running the Getreadabledatabase method.
1 New Mysqliteopenhelper (mcontext); 2 Mysqliteopenhelper.getreadabledatabase ();
View Code
Database Creation under Android