Insert bulk data to enable transactions
-(void) Insertdatawithcount: (Nsinteger) Count Isusetransaction: (BOOL) isuse{
if (isuse) {
To enable transactions manually
BOOL isError = NO;
@try {
Write code that may appear to be abnormal
[_database begintransaction];//manually open a transaction
for (int i=0; i<count; i++) {
NSString *idstr =[nsstring stringwithformat:@ "%d", I];
NSString *stname = [NSString stringwithformat:@ "student%d", I];
NSString *insertsql = @ "INSERT into student (Id,name) VALUES (?,?)";
if (![ _database Executeupdate:insertsql,idstr,stname]) {
NSLog (@ "Insert error:%@", _database.lasterrormessage);
}
}
}
@catch (NSException *exception) {
Catch to exception
NSLog (@ "error:%@", Exception.reason);
IsError = YES;
[_database rollback];//Rollback, back to the original state
}
@finally {
The code executes here, whether or not it is an exception.
if (Iserror==no) {
[_database commit];//commits the transaction, allowing the bulk operation to take effect
}
}
}else{
General operations
for (int i=0; i<count; i++) {
NSString *idstr =[nsstring stringwithformat:@ "%d", I];
NSString *stname = [NSString stringwithformat:@ "student%d", I];
NSString *insertsql = @ "INSERT into student (Id,name) VALUES (?,?)";
if (![ _database Executeupdate:insertsql,idstr,stname]) {
NSLog (@ "Insert error:%@", _database.lasterrormessage);
}
}
}
}
SQLite transaction Processing