当前位置: 首页 > news >正文

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://taox.l.blog.163.com/blog/static/48365573201262312756819/  (重点参考)

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

http://blog.csdn.net/yanfangjin/article/details/7610422   ios学习--SQLite常用函数  (最后一段,将事务的本质讲明白了)

 

转载于:https://www.cnblogs.com/ygm900/p/3511372.html

相关文章:

  • 【队列】【P2827】【NOIP2016D2T3】蚯蚓
  • java中Xml、json之间的相互转换
  • 新概念书店无非内容电商线下变体,西西弗终难逃被资本吞并命运?
  • android应用activity中调出输入法后界面调整问题的解决
  • watch深度监测
  • PHP-学习大规模高并发Web系统架构及开发推荐书籍
  • [caffe(二)]Python加载训练caffe模型并进行测试1
  • 【转】ini载入保存类,操作INI配置文件方便的很
  • PostgreSQL 连接的问题
  • 珍爱之礼 美妙感受
  • Python Flask-Mail环境变量配置
  • 内表生成XML简单实例
  • nginx基础
  • java 编程性能调优
  • 简单实现一个textarea自适应高度
  • [PHP内核探索]PHP中的哈希表
  • [译]前端离线指南(上)
  • 【干货分享】SpringCloud微服务架构分布式组件如何共享session对象
  • CODING 缺陷管理功能正式开始公测
  • Fundebug计费标准解释:事件数是如何定义的?
  • Kibana配置logstash,报表一体化
  • LeetCode18.四数之和 JavaScript
  • MD5加密原理解析及OC版原理实现
  • Next.js之基础概念(二)
  • Otto开发初探——微服务依赖管理新利器
  • tweak 支持第三方库
  • vue 配置sass、scss全局变量
  • vue从创建到完整的饿了么(11)组件的使用(svg图标及watch的简单使用)
  • 译有关态射的一切
  • 在weex里面使用chart图表
  • 最近的计划
  • 阿里云IoT边缘计算助力企业零改造实现远程运维 ...
  • 测评:对于写作的人来说,Markdown是你最好的朋友 ...
  • 扩展资源服务器解决oauth2 性能瓶颈
  • ​Python 3 新特性:类型注解
  • # C++之functional库用法整理
  • #Linux(make工具和makefile文件以及makefile语法)
  • #LLM入门|Prompt#3.3_存储_Memory
  • #快捷键# 大学四年我常用的软件快捷键大全,教你成为电脑高手!!
  • #中的引用型是什么意识_Java中四种引用有什么区别以及应用场景
  • $.type 怎么精确判断对象类型的 --(源码学习2)
  • %@ page import=%的用法
  • (10)STL算法之搜索(二) 二分查找
  • (2020)Java后端开发----(面试题和笔试题)
  • (4)事件处理——(6)给.ready()回调函数传递一个参数(Passing an argument to the .ready() callback)...
  • (Demo分享)利用原生JavaScript-随机数-实现做一个烟花案例
  • (NSDate) 时间 (time )比较
  • (八十八)VFL语言初步 - 实现布局
  • (力扣题库)跳跃游戏II(c++)
  • (六)vue-router+UI组件库
  • (四)Android布局类型(线性布局LinearLayout)
  • . ./ bash dash source 这五种执行shell脚本方式 区别
  • .NET 2.0中新增的一些TryGet,TryParse等方法
  • .net core 6 集成和使用 mongodb
  • .Net Core/.Net6/.Net8 ,启动配置/Program.cs 配置