当前位置:Gxlcms > 数据库问题 > 【FMDB】事务

【FMDB】事务

时间:2021-07-01 10:21:17 帮助过:1人阅读

  • {  
  •       
  •     BOOL isFile = NO;  
  •     NSString *DBFilePath = [self getDBPath];  
  •     NSFileManager *filaManager = [NSFileManager defaultManager];  
  •     if([filaManager fileExistsAtPath:DBFilePath])  
  •         isFile = YES;  
  •       
  •     self.iFmDb = [FMDatabase databaseWithPath:DBFilePath];        
  •     NSLog(@"openDB");  
  •     if (![self.iFmDb open])  
  •     {           
  •         return NO;  
  •     }  
  •     return YES;  
  • }  
  • -(void)testDBSpeed  
  • {  
  •     NSDate *date1 = [NSDate date];  
  •     [self insertData:500 useTransaction:NO];  
  •     NSDate *date2 = [NSDate date];  
  •     NSTimeInterval a = [date2 timeIntervalSince1970] - [date1 timeIntervalSince1970];  
  •     NSLog(@"不使用事务插入500条数据用时%.3f秒",a);  
  •     [self insertData:1000 useTransaction:YES];  
  •     NSDate *date3 = [NSDate date];  
  •     NSTimeInterval b = [date3 timeIntervalSince1970] - [date2 timeIntervalSince1970];  
  •     NSLog(@"使用事务插入500条数据用时%.3f秒",b);  
  •       
  • }  
  • - (void)insertData:(int)fromIndex useTransaction:(BOOL)useTransaction  
  • {  
  •     [self openDB];  
  •     if (useTransaction) {  
  •         [self.iFmDb beginTransaction];  
  •         BOOL isRollBack = NO;  
  •         @try {  
  •             for (int i = fromIndex; i<500+fromIndex; i++) {  
  •                 NSString *nId = [NSString stringWithFormat:@"%d",i];  
  •                 NSString *phone= [[NSString alloc] initWithFormat:@"phone_%d",i];  
  •                 NSString *strName = [[NSString alloc] initWithFormat:@"name_%d",i];  
  •                 NSString *roomID= [[NSString alloc] initWithFormat:@"roomid_%d",i];  
  •                 NSString *sql = @"INSERT INTO GroupPersonInfo(uid,phone,name,groupRoomId) VALUES (?,?,?,?)";  
  •                 BOOL a = [self.iFmDb executeUpdate:sql,nId,phone,strName,roomID];  
  •                 if (!a) {  
  •                     NSLog(@"插入失败1");  
  •                 }  
  •             }  
  •         }  
  •         @catch (NSException *exception) {  
  •             isRollBack = YES;  
  •             [self.iFmDb rollback];  
  •         }  
  •         @finally {  
  •             if (!isRollBack) {  
  •                 [self.iFmDb commit];  
  •             }  
  •         }  
  •     }else{  
  •         for (int i = fromIndex; i<500+fromIndex; i++) {  
  •             NSString *nId = [NSString stringWithFormat:@"%d",i];  
  •             NSString *phone= [[NSString alloc] initWithFormat:@"phone_%d",i];  
  •             NSString *strName = [[NSString alloc] initWithFormat:@"name_%d",i];  
  •             NSString *roomID= [[NSString alloc] initWithFormat:@"roomid_%d",i];  
  •             NSString *sql = @"INSERT INTO GroupPersonInfo(uid,phone,name,groupRoomId) VALUES (?,?,?,?)";  
  •             BOOL a = [self.iFmDb executeUpdate:sql,nId,phone,strName,roomID];  
  •             if (!a) {  
  •                 NSLog(@"插入失败2");  
  •             }  
  •         }  
  •     }  
  •     [self closeDB];  
  • }  
  • - (BOOL) closeDB  
  • {   
  •     NSLog(@"closeDB");  
  •     if (self.iFmDb)  
  •     {  
  •         return [self.iFmDb close];  
  •     }  
  •     return NO;  
  • }  
  •  

    【FMDB】事务

    标签:

    人气教程排行