Android Database User Guide (under)

Foreword

Has been said above, modifications to the table, in fact, the database upgrade, delete the table can be considered to upgrade ah, anyway, is changed, the database needs to be upgraded. 
In fact, there was a place so honestly determine the version of the database

public class DBHelper extends SQLiteOpenHelper {

    public static final String db_name = "test.db";

    public DBHelper(Context context, int version) {
        super(context, db_name, null, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table table1 (" +
                " _byte byte," +
                " _long long," +
                " _text text," +
                " _short short," +
                " _int int," +
                " _float float," +
                " _double double," +
                " _boolean boolean," +
                " _blob blob" +
                ")");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

}

Look at this construction method, the second argument is the database version number, you can apply at the time of the first release of this version number to fill in 1, and then the next version, the version number to fill in 2, but the database upgrade can not so simple the simple change of figures on the upgrade is completed.

It is a good time to introduce the method onCreate and onUpgrade.

onCreate: When creating the database, call the method, if the database already exists, this method will not be called.

onUpgrade: When the constructor that version, database version that is elevated when this method will be called.
For example, when the first release of the application, version here is 1, the second published application when you want to just create a new database table, then it needs to be upgraded database here so he passed a 2, this time on a method onUpgrade It will be called, but onCreate method can not be called, ah, because the database already exists.

One thing to mention here, only the database upgrade, not downgrade, or will be error of.

Create a database
above probably say, onCreate method and onUpgrade probably in what is invoked, so based on this principle. If we want to add or modify table table, we write in onUpgrade method. Here, for example, suppose we have released version of the database for the application 1.
Database established as follows:

public class DBHelper extends SQLiteOpenHelper {

    public static final String db_name = "test.db";

    public DBHelper(Context context, int version) {
        super(context, db_name, null, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table table1 (_text text)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

}

At this time, the way we call for:

// The current version of the database is 1 
new new the DBHelper (context, 1 )

In fact, we created a table named table1 table only, may wish to run later see, the database file is saved indata/data/包名/databases/xxx.db

The table also created the

Look at the fields in the table

No problem, then we started to upgrade the database, for example, the new table.

Database Upgrade - New Table

Next, we added a table, so you should write:

public class DBHelper extends SQLiteOpenHelper {

    public static final String db_name = "test.db";

    public DBHelper(Context context, int version) {
        super(context, db_name, null, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table table1 (_text text)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        switch (newVersion) {
            case 1:
            case 2:
                // 新增表table2
                db.execSQL("create table table2 (_text text)");
                break;
        }

    }

}

So when in use, you should use this:

// The current version of the database to 2 
new new the DBHelper (context, 2 )

Take a look?

Nothing issue

Database upgrade - Delete table

In fact, the main task is to call SQL statement is executed, it:

public class DBHelper extends SQLiteOpenHelper {

    public static final String db_name = "test.db";

    public DBHelper(Context context, int version) {
        super(context, db_name, null, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table table1 (_text text)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        switch (newVersion) {
            case 1:
            case 2:
                db.execSQL("create table table2 (_text text)");
            case 3:
                db.execSQL("drop table table2");
                break;
        }

    }

}

use:

// The current version of the database as 3 
new new the DBHelper (context, 3 )

Database upgrade - new field

A new field to the table:

public class DBHelper extends SQLiteOpenHelper {

    public static final String db_name = "test.db";

    public DBHelper(Context context, int version) {
        super(context, db_name, null, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table table1 (_text text)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        switch (newVersion) {
            case 1:
            case 2:
                db.execSQL("create table table2 (_text text)");
            case 3:
                db.execSQL("drop table table2");
            case 4:
                db.execSQL("alter table table1 ADD _long long");
                break;
        }

    }

}

use:

// The current version of the database for the 4 
new new the DBHelper (context, 4 )


Summary
is probably pretty much in this way the database upgrade, here is mainly the use of the sql statement against the database, if sql statement unfamiliar people, may need to query it before use.
Here probably say some common sql sentence structure:

New tables:

create table table name (field type)

Delete the table:

drop table 表名

 

Modify the table - add table fields

alter table add the table name field type

 

Modify the table - Delete field

alter table table name drop column field name

SQLite support normal SQL statements, just call SQLiteDatabase object execSQL method can operate directly on the database by the SQL statement

Here you can learn SQL statement Usage

Another point to note is that why onUpgrade method switch, not every case corresponds to a break, in fact, here is a little trick, when we use the database upgrade, this switch is often written like this:

    Switch (newVersion) {
             Case  . 1 :
                 // update. 1 
            Case  2 :
                 // Update 2 
            Case  . 3 :
                 // update. 3 
            Case  . 4 :
                 // update. 4 
            Case  . 5 :
                 // update. 5 
            Case  . 6 :
                 // update. 6 
            Case  . 7 :
                 // update 7 
            ...
                 BREAK ; 
        }

Because of this writing there is a benefit, suppose our application, the latest version has been upgraded to version 7, but some users are still using version 4, this time, if the user direct upgrade to the latest version 7, here's execution, it would be

Update 5 
Update 6 
Update 7

Put the previous update missing all caught up, which reads benefits.

This chapter is talking about database upgrade, the process is probably such a process, so be it!

---------------------
Disclaimer: This article is CSDN bloggers, "You lack imagination." Original article, follow the CC 4.0 by-sa copyright agreement, reproduced attach the original source link and this statement.
Original link: https: //blog.csdn.net/IT_XF/article/details/82684562

Guess you like

Origin www.cnblogs.com/Im-Victor/p/11332440.html