9.SQL文でのトランザクションの使用
1590 ワード
begin transaction
を開始とし、commit transacation
またはrollback transaction
で終了します. -> ->
//
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// 0.
[[SQLiteTool shareInstance] beginTransaction];
// 1.
// zhangsan -50 lisi +50
NSString *sql = @"update T_human set money = money - 50 where name = 'zhangsan'";
BOOL result = [[SQLiteTool shareInstance] excuteSQL:sql];
NSString *sql2 = @"update T_human set money = money + 50 where name = 'lisi'";
BOOL result2 = [[SQLiteTool shareInstance] excuteSQL:sql2];
NSLog(@"%i, %i", result, result2);
if (result && result2) {
//
[[SQLiteTool shareInstance] commitTransaction];
} else {
//
[[SQLiteTool shareInstance] rollBackTransaction];
}
}