当前位置:Gxlcms > 数据库问题 > FMDB批量插入数据

FMDB批量插入数据

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

从根本上来说,这是一种伪批量插入,但是速度却大大提升,几百上千条的数据基本0.04秒左右就能完成.如果不做的话,它会默认为每一条都做事务处理,所以很慢.

不多说了,上代码:

- (void)insertStudentsUseTransaction:(NSArray<StudentEntity *>*)students {

    [self.db open];
    [self.db beginTransaction];
    BOOL isRollBack = NO;
    @try {
        for (StudentEntity *studentEntity in students) {
            BOOL isSuccess = [self.db executeUpdate:@"insert into StuList(sid, user_id, realname, avatar_url, isyjmember) values(?,?,?,?,?)", studentEntity.sid, studentEntity.user_id, studentEntity.realname, studentEntity.avatar_url, studentEntity.isyjmember];
            NSLog(@"%@", isSuccess ? @"插入学生数据成功" : @"插入学生数据失败");
        }
    } @catch (NSException *exception) {
        isRollBack = YES;
        [self.db rollback];
    } @finally {
        if (!isRollBack) {
            [self.db commit];
        }
    }
    [self.db close];
}

 

FMDB批量插入数据

标签:事务处理   pen   插入   速度   mit   self   suse   情况   bool   

人气教程排行