zoukankan      html  css  js  c++  java
  • Android数据库使用指南(下)

    前言

    上面已经说了,对表进行修改,其实就是对数据库进行升级,删除表也算升级啊,反正就是发生变化,数据库就需要升级。 
    所以老实说其实有个地方决定了数据库的版本

    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) {
    
        }
    
    }

    看看这个构造方法,其中第二个参数就是数据库版本号,你可以在第一次发布应用的时候,这个版本号填写1,然后下一个版本,版本号填写2,但是数据库升级可没有那么简单的单纯改个数字就升级完毕了。

    是时候好好的介绍一下onCreate和onUpgrade方法了。

    onCreate:在创建数据库的时候,调用该方法,如果数据库已经存在了,这个方法将不会被调用。

    onUpgrade:当构造方法中的那个version,也就是数据库版本升高了的时候,这个方法才会被调用。
    比如,第一次发布应用的时候,这里的version是1,第二次发布应用的时候,刚好数据库要新建一个表,然后需要升级一下数据库,这里于是就传递了个2,这时候onUpgrade方法就会被调用了,不过onCreate方法可不会被调用了啊,因为数据库已经存在了。

    这里有一点要提一下,数据库只能升级,不能降级,不然会报错的。

    创建数据库
    上面大概说了,onCreate和onUpgrade方法大概是在什么时候被调用的,所以基于这个原理。如果我们要新增表或者修改表,我们就在onUpgrade方法中写。这里举个例子,假设我们已经发布了数据库版本为1的应用。
    数据库建立如下:

    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) {
        }
    
    }

    此时我们调用的方式为:

    // 当前数据库的版本为1
    new DBHelper(context, 1)

    其实我们就创建了一个表名为table1的表而已,不妨运行后来看看,数据库文件保存在data/data/包名/databases/xxx.db

    表也创建好了

    看看表中字段

    都没有问题,接下来,我们开始升级数据库,比如,新建表。

    数据库升级-新增表

    接下来,我们新增一个表,所以就应该这样写:

    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;
            }
    
        }
    
    }

    所以在用的时候,就应该这样用:

    // 当前数据库的版本为2
    new DBHelper(context, 2)

    看看?

    没啥问题

    数据库升级-删除表

    其实主要还是调用SQL语句执行任务,所以:

    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;
            }
    
        }
    
    }

    使用:

    // 当前数据库的版本为3
    new DBHelper(context, 3)

    数据库升级-新增字段

    给某个表新增字段:

    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;
            }
    
        }
    
    }

    使用:

    // 当前数据库的版本为4
    new DBHelper(context, 4)


    总结
    大概差不多就是以这种方式对数据库进行升级,这里主要是使用sql语句对数据库进行操作,如果对sql语句不熟悉的人,可能需要在使用前查询一下了。
    这里大概说几种常用的sql语句的结构:

    新增表:

    create table 表名 (字段 类型)

    删除表:

    drop table 表名

    修改表-新增表字段

    alter table 表名 add 字段 类型

    修改表-删除字段

    alter table 表名 drop column 字段名

    SQLite支持正常的SQL语句,只需要调用SQLiteDatabase对象的execSQL方法就可以直接通过SQL语句对数据库进行操作了

    大家可以在这里了解到SQL语句的用法

    还有一点要说明的就是,为啥onUpgrade方法中的switch,不是每个case都对应一个break,这里其实是一个小技巧,我们在使用数据库升级的时候,往往switch这里是这样写的:

        switch (newVersion) {
                case 1:
                    // 更新1
                case 2:
                    // 更新2
                case 3:
                    // 更新3
                case 4:
                    // 更新4
                case 5:
                    // 更新5
                case 6:
                    // 更新6
                case 7:
                    // 更新7
                ...
                    break;
            }

    因为这样写有一个好处,假设我们的应用,最新版已经升级到第7个版本了,但是有的用户还在用第4个版本,这时候,如果这个用户直接升级到最新的第7版,这里的执行,就会是

    更新5
    更新6
    更新7

    就把以前遗漏的更新全部赶上了,这就这样写的好处。

    本章主要就是讲数据库升级,流程大概就是这么个流程,就这样吧!

    ---------------------
    版权声明:本文为CSDN博主「你缺少想象力」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/IT_XF/article/details/82684562

  • 相关阅读:
    springMVC源码分析
    世界近代史二
    世界近代历史
    UVA
    UVA
    UVA
    Web 前端开发学习之路(入门篇)
    01 Linux入门介绍
    2. Python基本知识
    1. 初识Python
  • 原文地址:https://www.cnblogs.com/Im-Victor/p/11332440.html
Copyright © 2011-2022 走看看