程序员首页投稿(暂停使用,暂停投稿)Android开发

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

2016-05-13  本文已影响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;
    }
  }
}
第三版程序,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;
    }
  }
}

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

上一篇 下一篇

猜你喜欢

热点阅读