sqlite 批量插入, 重复插入(更新)

[FMDBManager inDatabase:^(FMDatabase *db) {
               [db shouldCacheStatements];
                //开始启动事务
                [db beginTransaction];
                BOOL isRollBack = NO;
                @try {
                    for (NSDictionary *dic in resultDic[@"events"]) {
                        //replace into 首先尝试插入数据到表中, 1. 如果发现表中已经有此行数据(根据主键或者唯一索引判断)则先删除此行数据,然后插入新的数据。 2. 否则,直接插入新数据。
                        
                        NSString *sql = [NSMutableString stringWithFormat:@"replace into events values('%@','%@','%@','%@','0','0')",dic[@"id"],dic[@"title"],dic[@"src"],dic[@"type"]];
                        BOOL a = [db executeUpdate:sql];
                        if (!a) {
                            NSLog(@"插入失败1");
                        }
                    }
                }
                @catch (NSException *exception) {
                    isRollBack = YES;
                    [db rollback];
                }
                @finally {
                    if (!isRollBack) {
                        [db commit];
                    }
                }
            }];
        }

 

posted @ 2015-11-19 19:52  levyleo  阅读(3763)  评论(0编辑  收藏  举报