[android] SQLite 数据库的升级 和 降级

public class SqliteHelp extends SQLiteOpenHelper {
	
	/*
	 * context:创建数据库所需的 上下文对象
	 * name: 数据库名字
	 * factory 游标 查询的时候使用
	 * version 指定数据库的版本高版本会自动更新 低版本的(自定义调用 铺面ongrade)
	 * */
	public SqliteHelp(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
	}
	
	// 数据库第一次使用的时候  执行
	@Override
	public void onCreate(SQLiteDatabase db) {
		Log.i("info", "数据库第一次使用的时候  执行");
		db.execSQL("create table info(name text)");
	}
	
	
	//数据库升级版本时候执行 
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		Log.i("info", "更新数据库的时候会调用这个方法" +oldVersion +newVersion);
		db.execSQL("drop table if exists aa");
		db.execSQL("create table aa(name text)");
	}
	
	// 降低数据库版本时调用 
	public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion){
		
	}
	
	// 开启 或  禁止   写入 日志 到 数据库	
	//setWriteAheadLoggingEnabled(boolean enabled)
	//Enables or disables the use of write-ahead logging for the database.
}

  

// 要在注册表 中注册  测试类
public class AndroidSqliteTest extends AndroidTestCase {
	
	private int Vision=1;
	
	private SqliteHelp sqliteHelp;
	private SQLiteDatabase db;
	private Cursor cursor;
	
	
	//启动 一个测试类时  
	//自动执行setUp(Runner调用)方法,可以对一些对象进行赋值
	@Override
	protected void setUp() throws Exception {
		super.setUp();
		Log.i("info", "在所有方法之前执行");
		// 配置 数据库的 创建信息
		sqliteHelp = new SqliteHelp(this.getContext(), "test.db", null, Vision);
		
		// 通过工具类来创建 数据库 对象
		//getReadableDatabase()   创建 和打开一个数据库
		//getWritableDatabase()   创建 和打开一个数据库  并且会帮你 创建  你定义过的 表 视图
		db = sqliteHelp.getWritableDatabase();
		
		// 可以 有 写入日志  和外键的支持 的特性
		//to enable features such as write-ahead logging or foreign key support.
		//sqliteHelp.onConfigure(db);
	}
	
	
	// 测试方法结束后会调用   比如关闭一个数据库的连接 时
	@Override
	protected void tearDown() throws Exception {
		super.tearDown();
		sqliteHelp.close();
		db.close();
		Log.i("info", "在所有方法之后执行");
	}
	
	// 添加操作
	public void insert() throws IOException {
		
		// 纯sql 来 插入数据
		db.execSQL("insert into info(name) values(?)",new Object[]{"hehe"});
		Log.i("info", "成功插入一条数据");
/*InputStream in = this.getContext().getResources().getAssets().open("bg.jpg"); int length =in.available(); byte[] b = new byte[length]; in.read(b);*/
/*ContentValues values = new ContentValues(); values.put("name", "不知道哦"); values.put("pic", b); long insert = db.insert("aa", null, values);*/ } //查询操作 public void query() { cursor = db.rawQuery("select * from info ", new String[]{}); while(cursor.moveToNext()){ String name = cursor.getString(cursor.getColumnIndex("name")); Log.i("info","查询成功"+ name); } } }

1.version =1 时;  创建一个数据库  并且只有一个 name字段 ;

  打印的日志为:

      

2.给表 一条插入 数据  并查询出来:

  打印的日志为:

      

3.升级数据库版本 给数据库(添加一个age字段)

   此时把测试类中的 version改为 version=2;  

  代表 第二版本

  1--->2 版本  或  直接  安装 2版本 修改代码为

    

  // 数据库第一次使用的时候  执行
	@Override
	public void onCreate(SQLiteDatabase db) {
		Log.i("info", "数据库第一次使用的时候  执行");
		db.execSQL("create table info(name text,age integer)");
	}
	
	//数据库升级版本时候执行 
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		Log.i("info", "数据库升级版本时候执行" +oldVersion +newVersion);
		//db.execSQL("drop table if exists info");
		if(oldVersion==1){
			db.execSQL("alter table info add column age integer");
		}
	}

  

打印的日志为:

  

在插入一条数据:

        

// 添加操作
	public void insert() throws IOException {
		// 纯sql 来 插入数据
		db.execSQL("insert into info(name,age) values(?,?)",new Object[]{"hehe","18"});
		Log.i("info", "成功插入一条数据");
	}

打印的日志为:

    

4.降低为 数据版本为 1 版本时:

  把 version =2 改为  version = 1

  2--->1; 

  在次改代码:   

    //数据库升级版本时候执行 
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		Log.i("info", "数据库升级版本时候执行" +oldVersion +newVersion);
		//db.execSQL("drop table if exists info");
		if(oldVersion==1){
			db.execSQL("alter table info add column age integer");
		}
	}
	
	// 降低数据库版本时调用 
	public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion){
		Log.i("info", " 降低数据库版本时调用 " +oldVersion +newVersion);
		try {
			String rename_sql = "alter table info rename to info_back";
			db.execSQL(rename_sql);
			Log.i("info", "改名成功");
	
			String sql_message = "create table info (name text)";
			db.execSQL(sql_message);
			Log.i("info", "重新建立 version =1 时的表结构成功");
				
			String sql_copy = "insert into info select name from info_back";
			db.execSQL(sql_copy);
			Log.i("info", "copy到用户数据到  version =1 的数据表中去");
				
			String drop_sql = "drop table if exists info_back";
			db.execSQL(drop_sql);
			Log.i("info", "把备份表drop掉");
				
		} catch (Exception e) {
			//失败
			Log.i("info", "降级失败,重新建立");
			String drop_old_table = "drop table if exists info";
			db.execSQL(drop_old_table);
		
		}
	}

   打印的日志为:

      

 

可以完成了一个简单的 升级和降级 ; 

  考虑放到云端(服务器)上去:

 降级的设计关键点
1、考虑云端要保存用户【自定义数据、行为习惯】。专业术语profile-->>提高用户黏度
2、考虑[当前]的最低版本要求-->>降低维护成本
3、尽可能本地的数据转移(所有新版本,都不删除字段)-->尽可能把未知变已知   try catch 
 

 

posted @ 2016-11-30 17:32  SoarSky  阅读(278)  评论(0编辑  收藏  举报