This article shares the data of batch updates in FMDB transactions in iOS for your reference. The specific content is as follows
Open the database (sqlite)
///Open the database+ (BOOL)openDataBase{ _TYDatabase = [[FMDatabase alloc]initWithPath:[self databasePath]]; if ([_TYDatabase open]) { return YES; } return NO; } ///Database path+ (NSString *)databasePath{ NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *dataPath = [documentPath stringByAppendingPathComponent:@""]; NSFileManager *fileM = [NSFileManager defaultManager]; if (![fileM fileExistsAtPath:dataPath]) { NSString *filePath = [[NSBundle mainBundle] pathForResource:@"TY" ofType:@"SQLite"]; [fileM copyItemAtPath:filePath toPath:dataPath error:nil]; } NSLog(@"%@",dataPath); return dataPath; }
Transactions
/** Transactions arraySql: SQL statement array */ - (void)beginTransaction:(NSArray *)arraySql; { //// static FMDatabase *_TYDatabase = nil; BOOL isOpen=[_TYDatabase open]; if (!isOpen) { NSLog(@"Download database failed!"); return; } ///Open the transaction [_TYDatabase beginTransaction]; BOOL isRollBack = NO; @try { for (int i = 0; i<; i++) { BOOL result = [_TYDatabase executeUpdate:arraySql[i]]; if (!result) { NSLog(@"Operation failed【%d】== SQL:%@",i,arraySql[i]); } } } @catch (NSException *exception) { isRollBack = YES; ///rollback [_TYDatabase rollback]; } @finally { if (!isRollBack) { ///submit [_TYDatabase commit]; } } [_TYDatabase close]; }
Multithreaded transactions
/** Multithreaded transactions arraySql: SQL statement array */ + (void)beginTransactionT:(NSArray *)arraySql{ FMDatabaseQueue *databaseQueue = [FMDatabaseQueue databaseQueueWithPath:[self databasePath]]; [databaseQueue inTransaction:^(FMDatabase *db, BOOL *rollback) { BOOL result = NO; for (int i = 0; i < ; i++) { result = [_TYDatabase executeUpdate:arraySql[i]]; } if (result) { NSLog(@"success"); } }]; }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.