android開發(24)使用SQLiteOpenHelper的onUpgrade實現數據庫版本升級


我這里說的數據庫版本指的是:我們的應用的程序的數據庫的用戶版本(user_version).比如說下面的情形:

2013年4月,我們第一次 發布了 我們的應用,數據庫版本是1。

2013年5月,我們第二次 發布了 我們的應用,數據庫版本是2。由於業務需要,我們更改了數據庫里的某個表的表結構。

這時候就有這樣的難題出現:

有些用戶已經下載了4月份的版本1,並且已經使用了,很多數據存儲在數據庫了,這個時候,他想安裝新的版本2,怎么辦? 怎么才能讓數據不丟失?

有的用戶直接裝了5月份的版本,那這些用戶就直接使用了新的表結構格式。

可能以后還有版本3,4,N,怎么保證“數據不丟失的情況下“讓用戶手機里的數據庫跟着升級?

------

我們記得SQLiteOpenHelper的onUpgrade方法,那么它是如何工作呢?我們該如何使用他?下面先說說使用它的方式。

解決方案:

我們在4月份數據庫建立時,使用下面的方式

public class MyDbHelper1 extends SQLiteOpenHelper{
	public final static int DB_VERSION = 1;
	public final static String DB_NAME = "mydb.db";
	public final String TABLE_NAME = "tbl_data";
	public final String COL_UID = "uid";
	public final String COL_ITSVALUE = "itsvalue";
	public final String COL_CREATEDDATE = "createddate";
	
	public MyDbHelper1(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String sql = "create table ["+TABLE_NAME+"] ( [uid] int identity primary key,[itsvalue] nvarchar(200),createddate TIMESTAMP default (datetime('now', 'localtime')) )";
		db.execSQL(sql);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		
	}
}

  我們看到,在這里,我們的onCreate方法里,寫個建表的語句,這個表有3個字段。

 

我們注意看下數據庫的版本

..............................

於是到了五月份,由於業務需要,我們想添加新的字段到這個表里。我們這樣寫代碼:

public class MyDbHelper2 extends SQLiteOpenHelper{
	public final static int DB_VERSION = 2;
	public final static String DB_NAME = "mydb.db";
	public final String TABLE_NAME = "tbl_data";
	public final String COL_UID = "uid";
	public final String COL_ITSVALUE = "itsvalue";
	public final String COL_CREATEDDATE = "createddate";
	public final String COL_DESC = "desc";
	
	
	public MyDbHelper2(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		//創建新的數據庫時。已經 有新列desc了。
		String sql = "create table ["+TABLE_NAME+"] ( [uid] int identity primary key,[itsvalue] nvarchar(200),createddate TIMESTAMP default (datetime('now', 'localtime')),[desc] nvarchar(300) )";
		db.execSQL(sql);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		if(oldVersion == 1 && newVersion == 2){
			//從版本1到版本2時,增加了一個字段 desc
			String sql = "alter table ["+TABLE_NAME+"] add [desc] nvarchar(300)";
			db.execSQL(sql);
		}
		
	}

  這個時候,onCreate方法里,是新的建立表的方式。而不同的是 onUpgrade方法里我們檢測了這樣的變化,如果 是從版本1到版本2,我們執行了一段”添加列的sql語句“。這段代碼,僅僅在符合這樣的場景里執行。

通過上面的方式,我們就完成了一次的數據庫升級的操作。android會判斷 數據庫的版本號,並自動的調用onUpgrade方法。

看看數據庫的版本

------------------

下面是一些擴展內容?

我們通過SQLite Expert看到的這個user_version是什么東西?經過在網絡上反復查到資料,這個其實是sqlite數據庫的"PRAGMA ".

看看描述:

PRAGMA schema_version; PRAGMA schema_version = integer ; PRAGMA user_version; PRAGMA user_version = integer ;
The pragmas schema_version and user_version are used to set or get the value of the schema-version and user-version, respectively. The schema-version and the user-version are big-endian 32-bit signed integers stored in the database header at offsets 40 and 60, respectively.
The schema-version is usually only manipulated internally by SQLite. It is incremented by SQLite whenever the database schema is modified (by creating or dropping a table or index). The schema version is used by SQLite each time a query is executed to ensure that the internal cache of the schema used when compiling the SQL query matches the schema of the database against which the compiled query is actually executed. Subverting this mechanism by using "PRAGMA schema_version" to modify the schema-version is potentially dangerous and may lead to program crashes or database corruption. Use with caution!
The user-version is not used internally by SQLite. It may be used by applications for any purpose.

在數據庫中,我們可以使用這樣寫 sql語句來查詢它:

    PRAGMA main.user_version 

或者來設置它的值

   PRAGMA main.user_version = 1

更多內容參考sqlite的官方描述。

 

參考:http://www.sqlite.org/pragma.html

http://dev.10086.cn/cmdn/bbs/thread-25063-1-1.html

http://blog.sina.com.cn/s/blog_6ffbcfdb0100vjhs.html

http://stackoverflow.com/questions/12778551/how-to-check-sqlite-database-file-and-get-my-defined-database-user-version

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM