如何升級只讀SQLite數據庫SQLiteException:不能只讀從版本X數據庫升級爲Y
我曾試圖改變SQLiteDatabase.OPEN_READWRITE);
和getReadableDatabase();
結果還是一樣。
當我想從版本1升級我的SQLite datatbase 2,我得到這個錯誤:
SQLiteException: Can't upgrade read-only database from version X to Y
在第1 verison我的應用程序中,我用這個代碼:
private static final String DB_NAME = "this_the_database";
private final Context context;
private static final int DATABASE_VERSION = 1;
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DATABASE_VERSION);
this.context = context;
String path = context.getDatabasePath(DB_NAME).getAbsolutePath();
SQLiteDatabase db = null;
try {
db = SQLiteDatabase.openDatabase(path, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) { // DB not found
db = getReadableDatabase();
db.close();
// Copy DB from asset folder.
copyDatabase(path);
}
if (db != null) {
db.close();
}
}
當我決定以此來upgarde數據庫,墜毀
@Override
public void onCreate(SQLiteDatabase db) {
new DatabaseHelper(context).getReadableDatabase();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DB_NAME);
onCreate(db);
}
編號:http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
請發佈完整的例外日誌 – Jack 2011-12-30 15:46:32