SQLite数据库--升级数据库最佳操作

作者: 大话程序 | 来源:发表于2016-05-13 20:32 被阅读2426次

    在程序发布以后,若我们再次开发升级了新的版本,此时,若数据库也增加了表或者原有的表需要新增字段,在不删除原数据库的情况下,进行数据库的升级

    模拟数据库升级案例

    第一版程序,只创建一张Book表

    MyDatabaseHelper中的代码

    public class MyDatabaseHelper extends SQLiteOpenHelper{
      //创建Book表的SQL语句
      public static final String CREATE_BOOK = "create table Book("
        + "id integer primary key autoincrement,"
        + "anthor text,"
        + "price real,"
        + "pages integer,"
        + "name text)";
      public MyDatabaseHelper(Context context, String name, CursorFactory factory, int version){
        super(context, name, factory, version);
      }
      @override
      public void onCreate(SQLiteDatabase db){
        //执行SQL语句,创建Book表
        db.execSQL(CREATE_BOOK );
      }
      @override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
      }
    }
    

    这样当用户安装了第一版程序时,其存储空间上自然就有了Book表

    第二版程序,数据库中新增一个Category表

    修改MyDatabaseHelper 中的代码

    public class MyDatabaseHelper extends SQLiteOpenHelper{
      //创建Book表的SQL语句
      public static final String CREATE_BOOK = "create table Book("
        + "id integer primary key autoincrement,"
        + "anthor text,"
        + "price real,"
        + "pages integer,"
        + "name text)";
    
      //创建Category表的SQL语句
      public static final String CREATE_CATEGORY = "create table Category("
      + "id integer primary key autoincrement,"
      + "category_name text,"
       + "category_code integer";
    
      public MyDatabaseHelper(Context context, String name, CursorFactory factory, int version){
        super(context, name, factory, version);
      }
      @override
      public void onCreate(SQLiteDatabase db){
        //执行SQL语句,创建Book表
        db.execSQL(CREATE_BOOK );
        //执行SQL语句,创建Category表
        db.execSQL(CREATE_CATEGORY );
      }
      @override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
        switch(oldVersion){
          case 1:
            db.execSQL(CREATE_CATEGORY);
          default;
        }
      }
    }
    
    • onCreate()方法中新增了一条创建Category表的语句:当用户没有使用个过这个程序时,直接安装了第二版的程序,首次使用的时候就会创建Book表和Category表
    • onUpgrade()方法中添加了一个switch判断,若用户使用了第一版的程序,再安装第二版的程序来覆盖第一版的程序,就会调用onUpgrade()方法,当旧版本的数据库版本是1,就会执行创建Category表的SQL语句
    第三版程序,Book表和Category表建立连接

    修改MyDatabaseHelper 中的代码

    public class MyDatabaseHelper extends SQLiteOpenHelper{
      //创建Book表的SQL语句
      public static final String CREATE_BOOK = "create table Book("
        + "id integer primary key autoincrement,"
        + "anthor text,"
        + "price real,"
        + "pages integer,"
        + "name text)";
    
      //创建Category表的SQL语句,较第二版程序比起来,新增了一个字段
      public static final String CREATE_CATEGORY = "create table Category("
      + "id integer primary key autoincrement,"
      + "category_name text,"
       + "category_code integer"
       + "category_id integer";
    
      public MyDatabaseHelper(Context context, String name, CursorFactory factory, int version){
        super(context, name, factory, version);
      }
      @override
      public void onCreate(SQLiteDatabase db){
        //执行SQL语句,创建Book表
        db.execSQL(CREATE_BOOK );
        //执行SQL语句,创建Category表
        db.execSQL(CREATE_CATEGORY );
      }
      @override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
        switch(oldVersion){
          case 1:
            db.execSQL(CREATE_CATEGORY);
          case 2:
            db.execSQL("alter table Book add column category_id integer");
          default;
        }
      }
    }
    
    • 创建Category表的SQL语句新增了一个字段,当用户没有使用过本程序时,直接安装的是第三个版本的程序就会执行onCreate()方法,实现了创建两个数据库表的功能
    • 当用户由第一个版本或者第二个版本升级到第三个版本的时候,就会执行onUpgrade()方法,注意没有break

    这样升级数据库就没有数据的丢失了......

    相关文章

      网友评论

        本文标题:SQLite数据库--升级数据库最佳操作

        本文链接:https://www.haomeiwen.com/subject/kormrttx.html