1.為什么使用數(shù)據(jù)庫(kù)版本控制钮糖?
當(dāng)應(yīng)用被用戶安裝后,開發(fā)者在新版本功能研發(fā)過(guò)程過(guò)枢析,對(duì)數(shù)據(jù)庫(kù)的表執(zhí)行新增玉掸、刪除、修改后醒叁,為了避免在安裝新版本的過(guò)程中造成新老版本的數(shù)據(jù)庫(kù)內(nèi)容結(jié)構(gòu)的沖突司浪,故而需要通過(guò)數(shù)據(jù)庫(kù)版本控制來(lái)進(jìn)行數(shù)據(jù)庫(kù)內(nèi)容的升級(jí)(onUpgrade)、降級(jí)(onDowngrade)操作來(lái)規(guī)避相關(guān)沖突把沼,要達(dá)成這些操作啊易,SQLiteOpenHelper.java是不可或缺的一個(gè)輔助類。
2.SQLiteOpenHelper.java類
官方API對(duì)SQLiteOpenHelper類的具體描述如下:
/**
* A helper class to manage database creation and version management.
* 一個(gè)用來(lái)管理數(shù)據(jù)庫(kù)的創(chuàng)建饮睬、版本管理的的輔助類
*
* You create a subclass implementing {@link #onCreate}, {@link #onUpgrade} and
* optionally {@link #onOpen}, and this class takes care of opening the database
* if it exists, creating it if it does not, and upgrading it as necessary.
* 你可以創(chuàng)建一個(gè)子類用來(lái)實(shí)現(xiàn){@link #onCreate}, {@link #onUpgrade}和
* 可選方法{@link #onOpen}租谈,該類負(fù)責(zé)打開已存在的數(shù)據(jù)庫(kù)數(shù)據(jù)庫(kù),
* 創(chuàng)建不存在的數(shù)據(jù)庫(kù)和進(jìn)行必要的升級(jí)捆愁。
* Transactions are used to make sure the database is always in a sensible state.
* 事務(wù)用來(lái)確保數(shù)據(jù)庫(kù)始終處于一個(gè)合理的狀態(tài)割去。
* This class makes it easy for {@link android.content.ContentProvider}
* implementations to defer opening and upgrading the database until first use,
* to avoid blocking application startup with long-running database upgrades.
* 這類便于{@link android.content.ContentProvider}在第一次使用之前實(shí)現(xiàn)推遲開放和升級(jí)數(shù)據(jù)庫(kù)窟却,
* 以避免阻塞和長(zhǎng)期數(shù)據(jù)庫(kù)升級(jí)應(yīng)用程序啟動(dòng)。
* For an example, see the NotePadProvider class in the NotePad sample application,
* in the samples/ directory of the SDK.
*
* Note: this class assumes monotonically increasing version numbers for upgrades.
* 注意:該類假定單調(diào)遞增的版本號(hào)進(jìn)行升級(jí)劫拗。
*/
由此可見间校,SQLiteOpenHelper類是一個(gè)用來(lái)創(chuàng)建、打開數(shù)據(jù)庫(kù)以及進(jìn)行數(shù)據(jù)庫(kù)版本控制的輔助類页慷,它為我們提供了onCreate、onUpgrade胁附、onDowngrade方法分別用來(lái)處理數(shù)據(jù)庫(kù)的創(chuàng)建酒繁、升級(jí)、降級(jí)操作控妻。
3.如何對(duì)數(shù)據(jù)庫(kù)進(jìn)行管理州袒?
在進(jìn)行具體操作之前,我們要先了解onCreate弓候、onUpgrade郎哭、onDowngrade三種方法的使用條件及其作用。
- onCreate(SQLiteDatabase db)
- 使用條件:
1.該方法只有在數(shù)據(jù)庫(kù)第一次打開的時(shí)候被調(diào)用菇存;
2.當(dāng)用戶選擇清除數(shù)據(jù)(數(shù)據(jù)庫(kù)文件放在內(nèi)存中)或選擇刪除存放在sd卡的外部數(shù)據(jù)庫(kù)文件時(shí)夸研,會(huì)重新調(diào)用該方法。 - 作用:
用于創(chuàng)建表等初始化操作依鸥。 - 注意:
在數(shù)據(jù)庫(kù)升級(jí)亥至、不清除數(shù)據(jù)時(shí),不會(huì)觸發(fā)該方法贱迟。 - onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
- 使用條件:
當(dāng)打開數(shù)據(jù)庫(kù)時(shí)傳入的版本號(hào)高于當(dāng)前版本號(hào)時(shí)姐扮,調(diào)用該方法。 - 作用:
用于升級(jí)數(shù)據(jù)庫(kù)版本衣吠。 - 注意:
1.第一次打開數(shù)據(jù)庫(kù)時(shí)茶敏,不會(huì)觸發(fā)該方法;
2.清除數(shù)據(jù)后重新進(jìn)入,不會(huì)觸發(fā)該方法缚俏。
3.數(shù)據(jù)庫(kù)版本號(hào)必須為大于等于0的整數(shù)惊搏,否則會(huì)報(bào)錯(cuò)。 - onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion)
- 使用條件:
當(dāng)打開數(shù)據(jù)庫(kù)時(shí)傳入的版本號(hào)低于當(dāng)前版本號(hào)時(shí)袍榆,調(diào)用該方法胀屿。 - 作用:
用于執(zhí)行對(duì)數(shù)據(jù)庫(kù)的降級(jí)操作。 - 注意:
1.如果不執(zhí)行降級(jí)操作包雀,會(huì)拋出異常;
2.數(shù)據(jù)庫(kù)版本號(hào)必須為大于等于0的整數(shù)宿崭,否則會(huì)報(bào)錯(cuò)。
通過(guò)對(duì)以上方法的使用條件才写、作用以及注意事項(xiàng)的分析葡兑,我們能夠很容易的理解對(duì)應(yīng)的方法在什么條件下會(huì)被使用奖蔓,以及在進(jìn)行對(duì)應(yīng)的數(shù)據(jù)庫(kù)管理操作時(shí)該在何種方法中進(jìn)行何種操作。
/**
* 1.該方法只有在數(shù)據(jù)庫(kù)第一次打開的時(shí)候觸發(fā);
* 2.當(dāng)用戶選擇清除數(shù)據(jù)(數(shù)據(jù)庫(kù)文件放在內(nèi)存中)或選擇刪除存放在sd卡的外部數(shù)據(jù)庫(kù)文件時(shí)讹堤,會(huì)重新觸發(fā)該方法吆鹤。
*
* 注:
* 在數(shù)據(jù)庫(kù)升級(jí)、不清除數(shù)據(jù)時(shí)洲守,不會(huì)觸發(fā)該方法疑务。
**/
@Override
public void onCreate(SQLiteDatabase db) {
}
/**
* 1.當(dāng)數(shù)據(jù)庫(kù)已存在且數(shù)據(jù)庫(kù)版本升高(新版本的數(shù)據(jù)庫(kù)版本號(hào)大于舊版本)時(shí),才會(huì)觸發(fā)該方法梗醇。
*
* 注:
* 1.第一次打開數(shù)據(jù)庫(kù)時(shí)知允,不會(huì)觸發(fā)該方法;
* 2.清除數(shù)據(jù)后重新進(jìn)入,不會(huì)觸發(fā)該方法叙谨。
**/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
/**
* 1.當(dāng)數(shù)據(jù)庫(kù)版本降級(jí)(新版本的數(shù)據(jù)庫(kù)版本號(hào)小于舊版本)時(shí)温鸽,會(huì)觸發(fā)該方法。
*
* 注:如果不執(zhí)行降級(jí)操作手负,會(huì)拋出異常涤垫。
**/
@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
super.onDowngrade(db, oldVersion, newVersion);
}
未完待續(xù)...