When working with databases in bulk, we often use transactions. A transaction is a unit of data that accesses and potentially updates the various data items of a database, and is the basic unit of recovery and concurrency control.
A transaction has four properties.
-Atomicity (atomicity): A transaction is an inseparable unit of work, and all operations included in the transaction are either done or not.
-Consistency (consistency): The transaction must be to change the database from one consistency state to another. Consistency is closely related to atomicity.
-Isolation (Isolation): Execution of one transaction cannot be disturbed by other transactions. That is, the operations inside a transaction and the data used are isolated from other transactions that are concurrently executing, and cannot interfere with each other concurrently.
-Persistence (Durability): Persistence is also known as permanence (permanence), which means that once a transaction is committed, its changes to the data in the database should be permanent. The next operation or failure should not have any effect on it.
SQLite supports transactions, and Fmdb also provides a good encapsulation of transactions.
SQL statements formed by transactions
Trigger transaction: BEGIN TRANSACTION
COMMIT TRANSACTION: Commit Transaction
ROLLBACK TRANSACTION: ROLLBACK TRANSACTION
SQLite itself is to support transactional operations, Fmdb as the upper package of SQLite also support the transaction, then said so many students do not understand the word "business" to explain it: usually once sqlite3_exec is a transaction, If you want to insert new data into the Stutent table in the database, the specific process of the transaction is to start a new thing, insert data, and commit the transaction, then when we want to insert 500 data into the table, if the normal operation will be executed 500 times "start new things The process of submitting a transaction by inserting data.
How to use Fmdb to manipulate transactions:
//Database ObjectsStaticFmdatabase *_db;/** User Database save path * /#define Kvideogatherdbfilepath [[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, NSUserDomainMask, YES) Lastobject] stringbyappendingpathcomponent:@ "Videogatherdb.sqlite"]+(void) Syschronizedatawitharr: (Nsarray*) dataarr{_db = [Fmdatabase Databasewithpath:kvideogatherdbfilepath];if([_db Open]) {[_db begintransaction];BOOLIsrollback =NO;@try{//Clear the data in the current table first! BOOLSuccessdelete = [_db executeupdate:@"DELETE from T_product"];if(Successdelete) { for(nsdictionary*productdict in Dataarr) {NSString*nsr_name = [Productdict objectforkey:@"Nsr_name"];NSString*org_id = [Productdict objectforkey:@"org_id"];NSString*NSRSBH = [Productdict objectforkey:@"NSRSBH"];if(! [_db executeupdate:@"INSERT into T_product (Nsr_name, org_id, NSRSBH) VALUES (?,?,?)", Nsr_name, org_id, NSRSBH]) {NSLog(@"%@", [_db LastError]); Break; } } }NSLog(@"Product table information inserted successfully!");Dispatch_async(Kmainqueue, ^{[Videogathertool productdatatosuccess];if(! [Videogathertool sysdataissuccess]) {Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (0.8* nsec_per_sec)), Dispatch_get_main_queue (), ^{[Svprogresshud showwithstatus:@"Syncing cloud data, keep your network open!"]; }); }Else{Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (1* nsec_per_sec)), Dispatch_get_main_queue (), ^{[Svprogresshud showsuccesswithstatus:@"cloud Data sync complete!"]; [Svprogresshud Dismisswithdelay:0.5]; [[NsuserdefaultsStandarduserdefaults] Setbool:YESforkey:@"Kisloginsysdata"]; }); } }); }@catch(NSException*exception) {Isrollback =YES; [_db rollback]; }@finally{if(!isrollback) {[_db commit]; } } }}
Ios-fmdb Transaction Operations SQLite database