SQLite transaction Processing

Source: Internet
Author: User
Tags sqlite

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.