[IOS 开发] sqlite 使用事务操作数据库


业务层代码:

//将解析的更新人员数据批量同步到数据库
+(void)operateCompUsers:(NSMutableArray*)operateCompUsers
{
    sqliteHelper *mysqlite = [[sqliteHelper alloc]init];
    if (operateCompUsers.count<=0) return;
    if([mysqlite openDatabase:@"ucab_db.db"])
    {
        NSMutableArray *transactionSql= [[NSMutableArray alloc]init];
        for (int i=0; i<operateCompUsers.count; i++)
        {
            CompUser *operateCompUser = [operateCompUsers objectAtIndex:i];
            
            if ([operateCompUser.operateType isEqualToString:@"0"])   //删除
            {
                NSString *nsstrSql = [[NSString alloc]initWithFormat:@"%@%@%@",@"delete from cloud_contacts where uid='",operateCompUser.uId,@"'"];
               
                    [mysqlite execSql:nsstrSql];
            }
            if ([operateCompUser.operateType isEqualToString:@"1"])   //可用,新增数据
            {
                //先将数据库中的数据删除
                NSString *nsstrSql = [[NSString alloc]initWithFormat:@"%@%@%@",@"delete from cloud_contacts where uid='",operateCompUser.uId,@"'"];
                [mysqlite execSql:nsstrSql];
                
                //再添加一次
                if (nil==operateCompUser.uId) operateCompUser.uId=@"";
                if (nil==operateCompUser.cn) operateCompUser.cn=@"";
                if (nil==operateCompUser.telephoneNumber) operateCompUser.telephoneNumber=@"";
                if (nil==operateCompUser.departmentNumber) operateCompUser.departmentNumber=@"";
                if (nil==operateCompUser.deptName) operateCompUser.deptName=@"";
                if (nil==operateCompUser.coNo) operateCompUser.coNo=@"";
                if (nil==operateCompUser.coName) operateCompUser.coName=@"";
                if (nil==operateCompUser.cuOrder) operateCompUser.cuOrder=@"";
                if (nil==operateCompUser.mobile) operateCompUser.mobile=@"";
                if (nil==operateCompUser.cuMail) operateCompUser.cuMail=@"";
                if (nil==operateCompUser.cuAllShow) operateCompUser.cuAllShow=@"";
                if (nil==operateCompUser.cuEntryStatus) operateCompUser.cuEntryStatus=@"";
                if (nil==operateCompUser.imagePath) operateCompUser.imagePath=@"";
                if (nil==operateCompUser.sort) operateCompUser.sort=@"";
                if (nil==operateCompUser.duty) operateCompUser.duty=@"";
                if (nil==operateCompUser.sex) operateCompUser.sex=@"0";  //性别默认为男
                
                //组sql语句
                NSString *strSql = [NSString stringWithFormat:@"insert into cloud_contacts (uid,cn,telephoneNumber,departmentNumber,deptName,coNo,coName,cuOrder,mobile,cuMail,cuAllShow,cuEntryStatus,imagePath,sort,duty,sex) values ('%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@');",operateCompUser.uId,operateCompUser.cn,operateCompUser.telephoneNumber,operateCompUser.departmentNumber,operateCompUser.deptName,operateCompUser.coNo,operateCompUser.coName,operateCompUser.cuOrder,operateCompUser.mobile,operateCompUser.cuMail,operateCompUser.cuAllShow,operateCompUser.cuEntryStatus,operateCompUser.imagePath,operateCompUser.sort,operateCompUser.duty,operateCompUser.sex];
               
                [transactionSql addObject:strSql];
            }
        }
        [mysqlite execInsertTransactionSql:transactionSql];
        [mysqlite closeDatabase];
    }
}

数据操作层(使用事务):

//执行插入事务语句
-(void)execInsertTransactionSql:(NSMutableArray *)transactionSql
{
    //使用事务,提交插入sql语句
    @try{
        char *errorMsg;
        if (sqlite3_exec(database, "BEGIN", NULL, NULL, &errorMsg)==SQLITE_OK)
        {
            NSLog(@"启动事务成功");
            sqlite3_free(errorMsg);
            sqlite3_stmt *statement;
            for (int i = 0; i<transactionSql.count; i++)
            {
                if (sqlite3_prepare_v2(database,[[transactionSql objectAtIndex:i] UTF8String], -1, &statement,NULL)==SQLITE_OK)
                {
                    if (sqlite3_step(statement)!=SQLITE_DONE) sqlite3_finalize(statement);
                }
            }
            if (sqlite3_exec(database, "COMMIT", NULL, NULL, &errorMsg)==SQLITE_OK)   NSLog(@"提交事务成功");
            sqlite3_free(errorMsg);
        }
        else sqlite3_free(errorMsg);
    }
    @catch(NSException *e)
    {
        char *errorMsg;
        if (sqlite3_exec(database, "ROLLBACK", NULL, NULL, &errorMsg)==SQLITE_OK)  NSLog(@"回滚事务成功");
        sqlite3_free(errorMsg);
    }
    @finally{}
}

参考:

iOS 中sqlite 事务提交代码  http://blog.csdn.net/hekunhotmail/article/details/8735882  (参考的比较多)



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值