Master Java
Packagecom.itheima.createdatabase;Importandroid.app.Activity;ImportAndroid.content.Context;Importandroid.database.sqlite.SQLiteDatabase;ImportAndroid.os.Bundle; Public classMainactivityextendsActivity {PrivateContext Mcontext; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Mcontext= This; //Create a helper class objectMysqliteopenhelper Mysqliteopenhelper =NewMysqliteopenhelper (Mcontext); //call the Getreadabledatabase method to initialize the creation of the databaseSqlitedatabase db =mysqliteopenhelper.getreadabledatabase (); } }
Creating a class in the same directory inherits from the database
Packagecom.itheima.createdatabase;ImportAndroid.content.Context;Importandroid.database.sqlite.SQLiteDatabase;Importandroid.database.sqlite.SQLiteDatabase.CursorFactory;ImportAndroid.database.sqlite.SQLiteOpenHelper; Public classMysqliteopenhelperextendsSqliteopenhelper { PublicMysqliteopenhelper (Context context) {//context: Contexts, Name: Database file names Factory: Used to create cursor objects, default to null//version: The release number of the database, starting with 1, if a change occurs, the Onupgrade method will be called, and after 4.0 can only rise cannot be Super(Context, "info.db",NULL, 1); } //the OnCreate method is called when the database is created for the first time; Especially suitable for the initialization of table structure, need to execute SQL statement; Sqlitedatabase DB can be used to execute SQL statements@Override Public voidonCreate (Sqlitedatabase db) {//execute a SQL statement that creates a table through SqlitedatabaseDb.execsql ("CREATE table info (_id integer PRIMARY key autoincrement,name varchar (20))"); } //Onupgrade Database version number will not be executed, especially for table structure changes@Override Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) { //Add a phone fieldDb.execsql ("ALTER TABLE info add phone varchar (11)"); }}
Teacher Notes
Under what circumstances do we use databases for data storage? When a large amount of data structure the same data needs to be stored.
MySQL sqlserver2000 sqlite embedded lightweight
Sqliteopenhelper
To create a database step:
1. To create a class-integrated sqliteopenhelper, you need to add a constructor method that implements the two methods OnCreate, Onupgrade
The parameters in the construction method are described in:
Context: Contexts, Name: Database file names Factory: Used to create cursor objects, default to NULL
Version: The release number of the database, starting with 1, if a change occurs, the Onupgrade method will be called, and after 4.0 can only rise cannot be
Super (context, "info.db", null,1);
2. Creating an object of this helper class, calling the Getreadabledatabase () method, will help us create a database that opens
3. Replication OnCreate and Onupgrdate methods:
The OnCreate method is called when the database is created for the first time; Especially suitable for the initialization of table structure, need to execute SQL statement; Sqlitedatabase DB can be used to execute SQL statements
Onupgrade database version number will not be executed, especially for table structure changes
The Getwritabledatabase and getreadabledatabase in the helper class object can help us get a database operand sqlitedatabase.
Difference:
Getreadabledatabase:
Try opening the database in read-write mode first, and if the disk is full, he tries to open the database as read-only again.
Getwritabledatabase:
Open the database directly to read and write, if the disk space is full, the error directly.
Creation of the Android database