public void Transation()
{
String sql1 ="update person set balance = balance - 1000 where _id = 1";
String sql2 ="update person set balance = balance + 1000 where _id = 2";
SQLiteDatabase db = helper.getWritableDatabase();
if(db.isOpen())
{
//开启一个事物
db.beginTransaction();
try {
db.execSQL(sql1);
int i = 10/0;
db.execSQL(sql2);
db.setTransactionSuccessful(); // 代表代码运行到这一行的时候是符合业务需求的,设置为事物成功
} finally
{
db.endTransaction(); //事物的结束,如果没有设置成功,那么会回滚你所做的任何操作,如果设置了则提交
}
db.close();
}
}
{
String sql1 ="update person set balance = balance - 1000 where _id = 1";
String sql2 ="update person set balance = balance + 1000 where _id = 2";
SQLiteDatabase db = helper.getWritableDatabase();
if(db.isOpen())
{
//开启一个事物
db.beginTransaction();
try {
db.execSQL(sql1);
int i = 10/0;
db.execSQL(sql2);
db.setTransactionSuccessful(); // 代表代码运行到这一行的时候是符合业务需求的,设置为事物成功
} finally
{
db.endTransaction(); //事物的结束,如果没有设置成功,那么会回滚你所做的任何操作,如果设置了则提交
}
db.close();
}
}