如果您正在使用事务并且 sql 操作失败,我知道它会回滚所有更改,但是它会在 finally 语句之后停止所有代码还是会继续运行其余代码?
下面的代码看起来是正确的还是我应该使用 catch 语句?是否可以出于测试目的模拟错误?
public static boolean updateSuccessful = false;
updateToVersion2();
//Does anything after here still run on error?
if(!updateSuccessful) {
deleteAndRecreateDatabase();
}
public void updateToDbVersion2() {
this.myDataBase.beginTransaction();
try {
this.myDataBase.execSQL("UPDATE myRecords SET column = 'newValue1' WHERE _id = 1");
this.myDataBase.execSQL("UPDATE myRecords SET column = 'newValue2' WHERE _id = 2");
this.myDataBase.setTransactionSuccessful();
// This is not reached on update error, right?
updateSuccessful = true;
} finally {
this.myDataBase.endTransaction();
}
}