Android database creation and upgrade database (middle)

Source: Internet
Author: User

In the previous article, a brief introduction to some of the basic concepts of Android database, then from this section, start with the Android database creation and upgrade.

As described above, Sqliteopenhelper is an abstract class, which is the best practice we use to create and upgrade databases. The following is a direct code to demonstrate the creation of the database operation.

<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: Database helper classes primarily complete database creation and upgrade */public class Mysqliteopenhelper extends Sqliteopenhelper {private static final Strin G db_name = "test.db";p rivate static final String table_name = "user";p rivate static final int db_version = 1;private stat IC final String id = "id";p rivate static final String name = "Name";p rivate static final String GENDER = "GENDER";p rivate Static final String telephone = "Telphone";//SQL statement to create database private static final string create_db_sql = "Create TABLE" + Tab Le_name+ "(" + ID + "Integer primary key AutoIncrement," + name+ "text NOT NULL," + GENDER + "text," + telephone +            "Integer" + ")";/** * Construction Method * * @param context * Context * @param name * Database name * @param factory * Cursor Factory * @paraM version * Database version number */public Mysqliteopenhelper (context context, String name,cursorfactory factory, int version) {Super (context, db_name, NULL, version);} /** * When a database file does not exist on the disk, the secondary class needs to create a new data using the */@Overridepublic void OnCreate (Sqlitedatabase db) {db.execsql (create_db_sql);} /** * already exists database, but the database version is not the same when called (upgrade) */@Overridepublic void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {//To Do auto-generated method stub}}</span>
If we run our program directly, we will find that we have not created our database. If you create and access a database, you need to call getreadabledatabase or getwritabledatabase to get a read-only or writable instance of the database. When we call execute Helper.getreadabledatabase () or helper.getwritabledatabase (), the DB instance is created.

Then using our SQLite Expert Professional 3 to open the secondary database, you will see the following data structure:

This completes the creation of the DB instance.

Below, the same approach describes the upgrade of our database version.

First, we define our SQL statement to upgrade the database, add a column to the user, and execute the SQL 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 the database version is different when called (upgrade)/@Overridepublic void Onupgrade (sqlitedatabase db, int oldversion, int newversion) { Db.execsql (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>
Then, after running, we will upgrade our database and open our database again, and we will find the data structure as follows:



This completes the upgrade logic for our database. Of course, in the actual development, upgrade does not simply execute one, need to judge the current version of the data, for different database versions to execute different SQL statements.

Of course, if we want to manage database creation and versioning operations directly, rather than using Sqliteopenhelper, you 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);d b.execsql ("CREATE table Student (ID integer primary key autoincrement, name Text,sex text,mobile integer) ");</span>

This also creates a test1.db database that you can create and then remember to use the database's exesql to create and delete tables based on execution.

This article is a brief introduction to this, are very shallow some things, but also in our Android basic used to create and upgrade the database of the optional solution.

Android database creation and upgrade database (middle)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.