天天看點

[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字段 ;

  列印的日志為:

      

[android] SQLite 資料庫的更新 和 降級

2.給表 一條插入 資料  并查詢出來:

  列印的日志為:

      

[android] SQLite 資料庫的更新 和 降級

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

  

列印的日志為:

  

[android] SQLite 資料庫的更新 和 降級

在插入一條資料:

        

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

列印的日志為:

    

[android] SQLite 資料庫的更新 和 降級

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

   列印的日志為:

      

[android] SQLite 資料庫的更新 和 降級

 

可以完成了一個簡單的 更新和降級 ; 

  考慮放到雲端(伺服器)上去:

 降級的設計關鍵點

1、考慮雲端要儲存使用者【自定義資料、行為習慣】。專業術語profile-->>提高使用者黏度

2、考慮[目前]的最低版本要求-->>降低維護成本

3、盡可能本地的資料轉移(所有新版本,都不删除字段)-->盡可能把未知變已知   try catch 

轉載于:https://www.cnblogs.com/liugch/p/6118738.html