Android database creation and upgrade (medium), android Database
In the previous article, I briefly introduced some basic concepts of the android database. From this section, I will create and upgrade the Android database.
As mentioned above, SQLiteOpenHelper is an abstract class, which is the best practice for creating and upgrading databases. The following code shows how to create a database.
<Span style = "font-size: 18px;"> package com. happy. db. db; import android. content. context; import android. database. sqlite. SQLiteDatabase; import android. database. sqlite. SQLiteDatabase. cursorFactory; import android. database. sqlite. SQLiteOpenHelper;/*** @ Description: The database auxiliary class mainly creates and upgrades the database */public class MySQLiteOpenHelper extends SQLiteOpenHelper {private static final String DB_NAME = "test. db "; private static final String TABLE_NAME =" user "; private static final int DB_VERSION = 1; private static final String ID =" id "; private static final String NAME = "name"; private static final String GENDER = "gender"; private static final String TELEPHONE = "telphone "; // CREATE database SQL statement private static final String CREATE_DB_ SQL = "CREATE TABLE" + TABLE_NAME + "(" + ID + "integer primary key autoincrement, "+ NAME +" text not null, "+ GENDER +" text, "+ TELEPHONE +" integer "+ ")"; /*** constructor ** @ param context * Context * @ param name * database name * @ param factory * cursor factory * @ param version * database version */public MySQLiteOpenHelper (context context, string name, CursorFactory factory, int version) {super (context, DB_NAME, null, version);}/*** when no database file exists on the disk, the auxiliary class needs to use */@ Overridepublic void onCreate (SQLiteDatabase db) mongodb.exe cSQL (CREATE_DB_ SQL) when creating a new data;}/*** already exists in the database, but when the database version is different, call (upgrade) */@ Overridepublic void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {// TODO Auto-generated method stub }</span>
If we run our program directly, we will find that our database has not been created. To create and access a database, call getReadableDatabase or getWritableDatabase to obtain read-only or writable instances of the database. After we call and execute helper. getReadableDatabase (); or helper. getWritableDatabase ();, the database instance is created.
Next, use SQLite Expert Professional 3 to open the database. The data structure is shown as follows:
In this way, the database instance is created.
The following describes how to upgrade the database version.
First, define the SQL statement for upgrading the database. Add a column for the user and execute the SQL statement in upgrade.
<Span style = "font-size: 18px; "> // upgrade database statement private static final String UPGRADE_ SQL =" ALTER TABLE "+ TABLE_NAME +" ADD COLUMN "+" money float ";/*** the database already exists, but when the database version is different, call (upgrade) */@ Overridepublic void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) mongodb.exe cSQL (UPGRADE_ SQL);} </span>
Then execute the following code in our program:
<Span style = "font-size: 18px;"> case R. id. bt_upgrade_db: // upgrade database helper = new MySQLiteOpenHelper (getApplicationContext (), "test. db ", null, 2); helper. getReadableDatabase (); break; </span>
In this way, after the operation, we will upgrade our database and open our database again, we will find that the data structure is as follows:
In this way, the upgrade logic of our database is completed. Of course, in actual development, the upgrade won't simply execute one statement. You need to judge the current data version and execute different SQL statements for different database versions.
Of course, if we want to directly manage database creation, opening, and version control operations, rather than using sqliteopenhelper, we can use the openOrCreateDataBase method of the application Context object to create the database itself.
<span style="font-size:18px;">Context context = getApplicationContext();SQLiteDatabase db = context.openOrCreateDatabase("test1.db",Context.MODE_PRIVATE, null);db.execSQL("create table student ( id integer primary key autoincrement , name text,sex text,mobile integer) ");</span>
In this way, the test1.db database can be created. After the database is created, you must use exeSQL to create and Delete tables.
This article briefly introduces some things, which are also an optional solution for creating and upgrading databases in Android.