我有一个应用程序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