实践--升级数据库最佳写法
2016-04-04 19:10
507 查看
当我们第一次建表的时候这样写
一段时间之后,我们要添加一张表,但是之前表的数据还不能删除,我们可以这样写
之后我们打算在第一个表中添加一个新的列字段
-这里有一个细节请注意,switch中,每一个case后面都没有使用break语句,这样可保证在跨版本升级的时候,每一次的数据库的修改都能被全部执行。比如说用户从第一版升级到第三版的时候。case 1和case 2中的逻辑就都会执行。
- 使用这种方式进行数据库的升级,不管版本怎么更新,都可以保证数据库的表结构是最新的,并且表中的数据也不会丢失。
public class MyDatabaseHelper extends SQLiteOpenHelper { private Context context; public static final String CREATE_STUDENT="create table book(id integer primary key autoincrement,name text,age integer,clazz text)"; public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); this.context=context; } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_STUDENT); Toast.makeText(context, "数据库创建成功", Toast.LENGTH_SHORT).show(); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
一段时间之后,我们要添加一张表,但是之前表的数据还不能删除,我们可以这样写
public class MyDatabaseHelper extends SQLiteOpenHelper { private Context context; public static final String CREATE_STUDENT="create table book(id integer primary key autoincrement,name text,age integer,clazz text)"; public static final String CREATE_HELLOWORD="create table helloword(id integer primary key autoincrement,name text,age integer,clazz text)"; public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); this.context=context; } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_STUDENT); //若是第一次建表,则将两个表都建立 db.execSQL(CREATE_HELLOWORD); Toast.makeText(context, "数据库创建成功", Toast.LENGTH_SHORT).show(); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { switch (oldVersion){ case 1: //若已经有第一个版本的数据库,我们只增加一个表就可以了 db.execSQL(CREATE_HELLOWORD); } } }
之后我们打算在第一个表中添加一个新的列字段
public class MyDatabaseHelper extends SQLiteOpenHelper { private Context context; //在其中新添加的sex字段 public static final String CREATE_STUDENT = "create table book(id integer primary key autoincrement,name text,age integer,clazz text,sex text)"; public static final String CREATE_HELLOWORD = "create table helloword(id integer primary key autoincrement,name text,age integer,clazz text)"; public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); this.context = context; } @Override public void onCreate(SQLiteDatabase db) { //若是第一次建表,则将两个表全部建立就好 db.execSQL(CREATE_STUDENT); db.execSQL(CREATE_HELLOWORD); Toast.makeText(context, "数据库创建成功", Toast.LENGTH_SHORT).show(); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { switch (oldVersion) { //若是已经安装过旧版本,根据相应版本号,添加相应的所缺的部分 case 1: db.execSQL(CREATE_HELLOWORD); case 2: db.execSQL("alter table book add column sex text"); } } }
-这里有一个细节请注意,switch中,每一个case后面都没有使用break语句,这样可保证在跨版本升级的时候,每一次的数据库的修改都能被全部执行。比如说用户从第一版升级到第三版的时候。case 1和case 2中的逻辑就都会执行。
- 使用这种方式进行数据库的升级,不管版本怎么更新,都可以保证数据库的表结构是最新的,并且表中的数据也不会丢失。
相关文章推荐
- 数据库引擎
- xmemcached简单使用
- SQL Server触发器创建、删除、修改、查看
- 吐槽 MySQL数据库jdbc操作,varchar类型占位符问题——单引号造孽
- MySQL 5.7 Distrib 5.7.8-rc 主从复制的简单配置,备忘
- SQL删除重复数据只保留一条
- My Sql 高效分页
- 学习二:MySql数据库的基本操作
- 《MySQL必知必会学习笔记》组合查询
- MS SQL Server数据库表int类型字段转换成定长varchar类型,长度不足前面补0
- MySQL数据库的CRUD操作
- MySQL数据库的连接
- MySQL数据库的连接
- 数据存储--SQLite事务
- 兼容数据库的设计思路
- Spring Data与MongoDB:不协调的设计
- sqlmap使用笔记
- Django1.9 models数据库同步问题
- SQLite简单使用
- oracle中建同名