[更新:解决方案 = 修复了 onUpgrade() 代码,该代码允许使用调试器成功测试数据库架构更改,而不是打包多个 apk]
我正在尝试测试 2 个 apk 以确保在我的应用更新部署到市场时会发生数据库更新。
Test1.apk:包含 sqlite schema_version = 1 Test2.apk:包含 sqlite schema_version = 2
两个 apk 都使用相同的密钥进行数字签名,使用 adb 我可以安装 Test1.apk。当我尝试安装 Test2.apk 时,我收到:
失败 [INSTALL_FAILED_ALREADY_EXISTS]
如何测试我的新架构更改?如果答案来自调试器,那么当调用 onUpgrade() 时,我的数据库始终处于锁定状态。
//////////////// This Code Fixed The onUpgrade() Issue /////////////////////
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//TODO: Write DB Update logic.
SQLiteDatabase upgradeDB = null;
if (newVersion > oldVersion){
Log.e(TAG, "NEWER VERSION DETECTED, DATABASE UPGRADE REQUIRED!");
InputStream inputStream = myContext.getResources().openRawResource(R.raw.dbscript_v2_0_0_0);
BufferedReader br2 = new BufferedReader(new InputStreamReader(inputStream), 1016688); //1016688 max
String buffer;
try {
db.beginTransaction();
//openDataBase();
while ((buffer = br2.readLine()) != null)
{
String[] execSql = buffer.split("\n");
execMultipleSQL(db, execSql);
}
db.setTransactionSuccessful();
Log.d(TAG, "onCreated sql: CREATED TABLES and INSERTED RECORDS");
} catch (SQLException e) {
Log.e("Error creating tables and debug data", e.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
db.endTransaction();
//db.close();
}
}
else
{
Log.e(TAG, "NO DATABASE UPGRADE DETECTED");
}
}
////////////////////////////////////////////////////////////////////////////
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//TODO: Write DB Update logic.
SQLiteDatabase upgradeDB = null;
if (newVersion > oldVersion){
Log.e(TAG, "NEWER VERSION DETECTED, DATABASE UPGRADE REQUIRED!");
InputStream inputStream = myContext.getResources().openRawResource(R.raw.dbscript_v2_0_0_0);
BufferedReader br2 = new BufferedReader(new InputStreamReader(inputStream), 1016688); //1016688 max
String buffer;
try {
String myPath = DATABASE_PATH + DATABASE_NAME;
myDataBase = SQLiteDatabase.openOrCreateDatabase(DATABASE_PATH + DATABASE_NAME, null);
myDataBase.beginTransaction();
//openDataBase();
while ((buffer = br2.readLine()) != null)
{
String[] execSql = buffer.split("\n");
execMultipleSQL(myDataBase, execSql);
}
myDataBase.setTransactionSuccessful();
//close();
Log.d(TAG, "onCreated sql: CREATED TABLES and INSERTED RECORDS");
} catch (SQLException e) {
Log.e("Error creating tables and debug data", e.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
myDataBase.endTransaction();
myDataBase.close();
}
}
else
{
Log.e(TAG, "NO DATABASE UPGRADE DETECTED");
}
}
public void openDataBase() throws SQLException {
try
{
/* When the database is 1st installed on the device, this helper class creates a blank database using getWritableDatabase().
* The actual database is then populated using the SQLiteDatabase.openDatabase(PATH, null, Open_ReadWrite) to exec the sql.
*
* PROBLEM - SQLiteDatabase.openDatabase() Does not call onUpgrade() so we're
* unable to update the database for future versions
*
* SOLUTION - Check to see if the dbExists, if not use SQLiteDatabase.openDatabase()
* because it will create the DB from scripts, we don't care about onUpgrade();
* If the db does exist, call getWritableDatabase() to invoke onUpgrade checks for
* future releases where the SCHEMA_VERSION is greater than the current db SCHEMA_VERSION
*/
boolean dbExist = checkDataBase();
if(!dbExist){
/* Calls: DOES NOT call onUpgrade()
* Errors if db exists and there are db changes (New Columns/Tables), so use the below getWritableDatabase() instead */
String myPath = DATABASE_PATH + DATABASE_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
else
{
/* Calls: onUpgrade()
* Errors if db does not exist, so use the above instead */
myDataBase = this.getWritableDatabase();
}
}
catch (Exception e) {
Log.e(TAG, "Error SQLiteFactoryAdapter openDataBase " + e.toString());
}
}