複製我使用本教程: Tutorial Custom database升級SQLite數據庫,我是從資產的文件夾(安卓)
我從資產文件夾複製數據庫,並將其複製到我的設備(或模擬器)。一切都是正確的。我從DDMS的角度看到我的數據庫。但我想ASLO升級我的數據庫有時,所以我所做的:
super(context, DB_NAME, null, 2); //changed version from 1 to 2
和修改onUpgrade方法:
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if(newVersion > oldVersion){
this.myContext.deleteDatabase(DB_NAME);
try {
this.copyDataBase();
} catch (IOException e) {
e.printStackTrace();
}
}
}
但運行後我還有我的設備上舊版本的數據庫。我如何刪除舊版本的數據庫並複製新的。 DB_NAME是我的數據庫名稱(帶有格式,但不包含路徑),copyDataBase()是將數據庫複製到設備(以及它工作)的方法。
我貼我的所有代碼:
public class DataBaseHelper extends SQLiteOpenHelper{
private static String DB_PATH = "/data/data/sitcom.quiz/databases/";
private static String DB_NAME = "sitcoms.sqlite";
private SQLiteDatabase myDataBase;
private final Context myContext;
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 4);
this.myContext = context;
}
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing - database already exist
}else{
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
//database does't exist yet.
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException{
//Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.d("adas", "dasd");
if(newVersion > oldVersion){
String myPath = DB_PATH + DB_NAME;
this.myContext.deleteDatabase(myPath);
try {
this.copyDataBase();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
和我的活動:
DataBaseHelper myDbHelper = new DataBaseHelper(this);
myDbHelper = new DataBaseHelper(this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
}catch(SQLException sqle){
throw sqle;
}
感謝您是否可以給我一個理由或只能暗示爲什麼別工作。
是的,你是對的。我做了什麼 ?我使用共享首選項來簽署新版本的數據庫,並在使用此對象之前刪除數據庫。謝謝。 – kolek
@ kolek:你能與大家分享解決方案嗎?我也需要類似的東西。 –
爲什麼不分享解決方案,Kolek?請,很多人需要它。 –