一.概況
SQLite數據庫的數據升級與降級的問題主要是要關注SQLiteOpenHelper這一個抽象的類
(ps:SQLiteOpenHelper類具體的介紹,請瀏覽本人的前一章博客,鏈接:http://www.cnblogs.com/zhaoyucong/p/6113911.html)
主要的三個方法:
onCreate()該方法是在你沒有安裝過(第一次運行)的時候執行,這時可以在這個函數中完成初始的數據表的創建
onUpgrade()該方法是在你安裝過的情況下,對數據繼續更新的時候執行,這時可以在這個函數完成數據庫版本升級帶來的舊版本的兼容問題,以及數據遷移問題。
onDowngrade()該方法是在現逆向降級(如應用由版本號4降級安裝版本號為3的包)時必須重寫的方法,如果應用降級覆蓋安裝時沒有重寫該方法則會崩潰。
二.例子(主要呈現的是不管用戶對使用的版本是(升級/降級/重新安裝)的情況下,我們數據庫的數據仍然保存着一致的)
版本1.0初始化的代碼如下:
public class MyDatabaseOpenHelper extends SQLiteOpenHelper { private static final String db_name = "mydata.db"; // 數據庫名稱 private static final int version = 1; // 數據庫版本 public MyDatabaseOpenHelper(Context context) { super(context, db_name, null, version); } //該方法第一次運行才會執行 public void onCreate(SQLiteDatabase db) { //沒有數據庫打印日記
//這里必須要寫好最早的版本-現在版本的建表,要考慮還沒有裝過舊版本(直接安裝最新版本)的用戶的情況 Log.i("Log","第一次運行,沒有數據庫,生成數據庫"); //建表語句 String sql_message = "create table t_message (id int primary key,userName varchar(50),lastMessage varchar(50),datetime varchar(50))"; //執行建表語句 db.execSQL(sql_message); } //數據庫存更新才會執行 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.i("updateLog","數據庫更新了!"); } }
調用代碼如下:
public class MainActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MyDatabaseOpenHelper helper = new MyDatabaseOpenHelper(MainActivity.this); helper.getWritableDatabase().close(); } }
升級版本為2.0(為創建的表添加內容)
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //判斷用戶當前安裝的本是不是1.0版本 if(oldVersion == 1){ String sql_init_1 = "insert into t_message values (1,'小明','內容1','昨天')"; String sql_init_2 = "insert into t_message values (2,'小紅','內容2','今天')"; db.execSQL(sql_init_1); db.execSQL(sql_init_2); Log.i("Log", "從1.0升級到2.0,升級成功!"); }
//如果還有更老的版本就繼續判斷,維護版本兼容性 }
降級版本1.0
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { try { //第一、先把t_message 未來的表,改名 String rename_sql = "alter table t_message rename to t_message_bak"; db.execSQL(rename_sql); Log.i("down", "1.改名成功"); //第二、建立1.0的表結構 String sql_message = "create table t_message (id int primary key,userName varchar(50),lastMessage varchar(50),datetime varchar(50))"; db.execSQL(sql_message); Log.i("down", "2.建立1.0表結構成功"); //第三、把備份的數據,copy到 新建的1.0的表 String sql_copy = "insert into t_message select id,userName,lastMessage,datetime from t_message_bak"; db.execSQL(sql_copy); Log.i("down", "3.copy到用戶數據到 1.0的表"); //第四、把備份表drop掉 String drop_sql = "drop table if exists t_message_bak"; db.execSQL(drop_sql); Log.i("down", "4.把備份表drop掉"); } catch (Exception e) { //失敗 Log.i("Log", "降級失敗,重新創建"); String sql_drop_old_table = "drop table if exists t_message"; String sql_message = "create table t_message (id int primary key,userName varchar(50),lastMessage varchar(50),datetime varchar(50))"; String sql_init_1 = "insert into t_message values (1,'小明','內容1','昨天')"; String sql_init_2 = "insert into t_message values (2,'小紅','內容2','今天')"; db.execSQL(sql_drop_old_table); db.execSQL(sql_message); db.execSQL(sql_init_1); db.execSQL(sql_init_2); } }