我有一個應用程序v1.0,它使用保存在資產文件夾中的本地數據庫...我已將應用程序更新到版本1.1,在新版本中內部數據庫有不同的條目。當我更新應用程序時,資產文件夾文件不會更新
不幸的是,安裝的應用程序繼續使用版本1.0的數據庫,我必須卸載應用程序並再次安裝以使用最新版本的數據庫。
真的是一件壞事迫使買家撤銷應用程序並重新安裝新版本...爲什麼資產文件沒有用新版本更新?
編輯
我明白,這個問題發生,因爲數據庫從資產文件中創建且僅當尚未創建,這是我DatabaseHelper類
public class DataBaseHelper extends SQLiteOpenHelper {
private static String TAG = "DataBaseHelper";
private static String DB_PATH = "";
private static String DB_NAME = "MyDatabaseFile";
private SQLiteDatabase mDataBase;
private final Context mContext;
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 1);
DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
this.mContext = context;
}
public void createDataBase() throws IOException {
// If database not exists copy it from the assets
boolean mDataBaseExist = checkDataBase();
if (!mDataBaseExist) {
this.getReadableDatabase();
this.close();
try {
copyDataBase();
Log.e(TAG, "createDatabase database created");
} catch (IOException mIOException) {
throw new Error("ErrorCopyingDataBase");
}
}
}
private boolean checkDataBase() {
File dbFile = new File(DB_PATH + DB_NAME);
// Log.v("dbFile", dbFile + " "+ dbFile.exists());
return dbFile.exists();
}
private void copyDataBase() throws IOException {
InputStream mInput = mContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream mOutput = new FileOutputStream(outFileName);
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(mBuffer)) > 0) {
mOutput.write(mBuffer, 0, mLength);
}
mOutput.flush();
mOutput.close();
mInput.close();
}
public boolean openDataBase() throws SQLException {
String mPath = DB_PATH + DB_NAME;
// Log.v("mPath", mPath);
mDataBase = SQLiteDatabase.openDatabase(mPath, null,
SQLiteDatabase.CREATE_IF_NECESSARY);
// mDataBase = SQLiteDatabase.openDatabase(mPath, null,
// SQLiteDatabase.NO_LOCALIZED_COLLATORS);
return mDataBase != null;
}
@Override
public synchronized void close() {
if (mDataBase != null)
mDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
我怎麼能當我在PlayStore上升級應用程序時,避免此問題,而不重命名數據庫文件?
好的。我更新了我的問題。 – KinkLore
@KinkLore:我建議用'SQLiteAssetHelper'直接替換那個代碼。 – CommonsWare