處理數據庫的版本是應用程序開發的非常重要的組成部分:
DatabaseHelper.java
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 4;
private static final String DATABASE_NAME = "database.db";
private static final String TAG = DatabaseHelper.class.getName();
private static DatabaseHelper mInstance = null;
private final Context context;
private DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
public static synchronized DatabaseHelper getInstance(Context ctx) {
if (mInstance == null) {
mInstance = new DatabaseHelper(ctx.getApplicationContext());
}
return mInstance;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(BookEntry.SQL_CREATE_BOOK_ENTRY_TABLE);
// The rest of your create scripts go here.
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.e(TAG, "Updating table from " + oldVersion + " to " + newVersion);
// You will not need to modify this unless you need to do some android specific things.
// When upgrading the database, all you need to do is add a file to the assets folder and name it:
// from_1_to_2.sql with the version that you are upgrading to as the last version.
try {
for (int i = oldVersion; i < newVersion; ++i) {
String migrationName = String.format("from_%d_to_%d.sql", i, (i + 1));
Log.d(TAG, "Looking for migration file: " + migrationName);
readAndExecuteSQLScript(db, context, migrationName);
}
} catch (Exception exception) {
Log.e(TAG, "Exception running upgrade script:", exception);
}
}
@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
private void readAndExecuteSQLScript(SQLiteDatabase db, Context ctx, String fileName) {
if (TextUtils.isEmpty(fileName)) {
Log.d(TAG, "SQL script file name is empty");
return;
}
Log.d(TAG, "Script found. Executing...");
AssetManager assetManager = ctx.getAssets();
BufferedReader reader = null;
try {
InputStream is = assetManager.open(fileName);
InputStreamReader isr = new InputStreamReader(is);
reader = new BufferedReader(isr);
executeSQLScript(db, reader);
} catch (IOException e) {
Log.e(TAG, "IOException:", e);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
Log.e(TAG, "IOException:", e);
}
}
}
}
private void executeSQLScript(SQLiteDatabase db, BufferedReader reader) throws IOException {
String line;
StringBuilder statement = new StringBuilder();
while ((line = reader.readLine()) != null) {
statement.append(line);
statement.append("\n");
if (line.endsWith(";")) {
db.execSQL(statement.toString());
statement = new StringBuilder();
}
}
}
}
一個例子項目是在同一鏈路還提供。我假設你已經有AppDbHelper類延伸SQLiteOpenHelper
。當你繼承它,你將需要實現onCreate
和onUpgrade
方法。
當onCreate
和onUpgrade
方法稱爲
onCreate
當新安裝的應用程序調用。
onUpgrade
當應用程序更新時調用。
組織數據庫版本 我管理類方法中的版本。創建接口遷移的實現。例如。對於第一版本創建MigrationV1
類,第二版本創建MigrationV1ToV2
(這些是我的命名約定)
public interface Migration {
void run(SQLiteDatabase db);//create tables, alter tables
}
實施例遷移:
public class MigrationV1ToV2 implements Migration{
public void run(SQLiteDatabase db){
//create new tables
//alter existing tables(add column, add/remove constraint)
//etc.
}
}
- 使用遷移類
onCreate
:由於onCreate
將被調用時應用程序新安裝,我們還需要執行所有遷移(數據庫版本更新)。所以onCreate
將是這樣的:
public void onCreate(SQLiteDatabase db){
Migration mV1=new MigrationV1();
//put your first database schema in this class
mV1.run(db);
Migration mV1ToV2=new MigrationV1ToV2();
mV1ToV2.run(db);
//other migration if any
}
onUpgrade
:當應用程序已經安裝此方法將被調用,並更新到新的應用程序版本。如果應用程序包含任何數據庫更改,則將所有數據庫更改置於新的遷移類和增量數據庫版本中。
例如,假設用戶已安裝具有數據庫版本1的應用程序,並且現在數據庫版本更新爲2(保存在MigrationV1ToV2
中的所有模式更新)。現在,當應用程序升級,我們需要通過應用數據庫模式變化MigrationV1ToV2
升級數據庫是這樣的:
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion < 2) {
//means old version is 1
Migration migration = new MigrationV1ToV2();
migration.run(db);
}
if (oldVersion < 3) {
//means old version is 2
}
}
注:所有的升級(在onUpgrade
提到的)中的數據庫模式應onCreate
執行
[This](https://stackoverflow.com/a/3424444/3681880)是另一個有趣的解決方案,但到目前爲止,我見過的最強大的版本是[here](https://riggaroo.co.za/Android的源碼數據庫 - 使用 - onupgrade-正確/)。 – Suragch