业务层代码:

//将解析的更新人员数据批量同步到数据库
+(void)operateCompUsers:(NSMutableArray*)operateCompUsers
{
sqliteHelper *mysqlite = [[sqliteHelper alloc]init];
if (operateCompUsers.count<=) return;
if([mysqlite openDatabase:@"ucab_db.db"])
{
NSMutableArray *transactionSql= [[NSMutableArray alloc]init];
for (int i=; i<operateCompUsers.count; i++)
{
CompUser *operateCompUser = [operateCompUsers objectAtIndex:i]; if ([operateCompUser.operateType isEqualToString:@""]) //删除
{
NSString *nsstrSql = [[NSString alloc]initWithFormat:@"%@%@%@",@"delete from cloud_contacts where uid='",operateCompUser.uId,@"'"]; [mysqlite execSql:nsstrSql];
}
if ([operateCompUser.operateType isEqualToString:@""]) //可用,新增数据
{
//先将数据库中的数据删除
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=@""; //性别默认为男 //组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 = ; i<transactionSql.count; i++)
{
if (sqlite3_prepare_v2(database,[[transactionSql objectAtIndex:i] UTF8String], -, &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常用函数  (最后一段,将事务的本质讲明白了)

iOS sqlite 使用事务操作数据库的更多相关文章

  1. sqlite:多线程操作数据库“database is locked”解决方法(二)

    上一篇博客<sqlite:多线程操作数据库“database is locked”解决方法>通过注册延时函数的方法来处理数据库被锁的问题.此方法固然能解决问题,但是在多个线程向数据库写入大 ...

  2. iOS sqlite C语言操作

    利用周六时间看了一下关于sqlite的知识.在这记录一下.看的传智播客视频 对数据的操作基本上就是增删改查: static sqlite3 *db; //声明一个数据库 @implementation ...

  3. JavaWeb_(Spring框架)整合Mybatis加入事务操作数据库

    整合Mybatis a)导包: i.Spring:基本包.aop.aspects.jdbc.tx.test: ii.Mybatis:mybatis-3.4.6 iii.整合包:mybatis-spri ...

  4. android: SQLite使用 SQL 操作数据库

    虽然 Android 已经给我们提供了很多非常方便的 API 用于操作数据库,不过总会有一些 人不习惯去使用这些辅助性的方法,而是更加青睐于直接使用 SQL 来操作数据库.这种人 一般都是属于 SQL ...

  5. iOS sqlite 的各种操作

    iOS --SQL的增加.删除.查找.修改 iOS对于数据库的操作:增加.删除.查找.修改 首先需要创建一个数据库:本程序的数据库是在火狐浏览器里的插件里写的微量型数据库 火狐找查找SQLite Ma ...

  6. sqlite:多线程操作数据库“database is locked”解决方法

    1. 使sqlite支持多线程(不确定是否非加不可,暂且加上,以备后患) 可以在编译时/启动时/运行时选择线程模式,参考:http://www.cnblogs.com/liaj/p/4015219.h ...

  7. [PHP] - PDO事务操作

    PHP使用PDO事务操作数据库. 参考文章: http://php.ncong.com/mysql/pdo/pdo_shiwu.html 上代码: <!doctype html> < ...

  8. c#操作数据库的增删改查语句及DataGridView简单使用

    下面是要用户名和密码连接数据库的操作: 一.定义连接字符串,用来链接SQL Server string str_con = "server=.(服务器名称一般为 . );database=W ...

  9. andorid SQLite数据库的增删改查 和事务操作

    .xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android ...

随机推荐

  1. java垃圾回收的回收器

    回收器的种类: --串行(–XX:+UseSerialGC ) Out ofBox算法,年轻代串行复制,年老代串行标记整理,主要用于桌面应用 --并行(–XX:+UseParallelGC ) 年轻代 ...

  2. like语句防止SQL注入

    mysql: select * from test where school_name like concat('%',${name},'%')   oracle: select * from tes ...

  3. 11 Go 1.11 Release Notes

    Go 1.11 Release Notes Introduction to Go 1.11 Changes to the language Ports WebAssembly RISC-V GOARC ...

  4. Java工具库:

    1.  重试框架: https://docs.spring.io/spring-batch/trunk/reference/html/retry.html <dependency> < ...

  5. css中width和padding-top实现正方形

    .div{ width: 100%; height: 0; padding-top: 100% } 这个时候,padding-top的值与width相等,所以可以让div宽高一样,值为width的值

  6. conda管理python包

    参考:http://blog.sina.com.cn/s/blog_c3c116470102wlv5.html 查看python,numpy,scipy,matplotlib的版本及安装位置: htt ...

  7. java 多态缺陷

    一,会覆盖私有方法 package object; class Derive extends Polymorphism{ public void f1() { System.out.println(& ...

  8. 【转】MySQL安全配置介绍

    一.前言 很多文章中会说,数据库的权限按最小权限为原则,这句话本身没有错,但是却是一句空话.因为最小权限,这个东西太抽象,很多时候你并弄不清楚具体他需要哪些权限. 现在很多mysql用着root账户在 ...

  9. 解决MySQL忘记root密码

    网上有很多关于忘记MySQL root密码的一些文章,里面都有写怎么去解决,但有时觉得写得太恶心,要么一字不漏的抄别人的,要么就说得不清不楚,好了,不吐槽了,以下是解决的整个过程. 首先我们要知道忘记 ...

  10. 【论文阅读】HydraPlus-Net: Attentive Deep Features for Pedestrian Analysis

    转载请注明出处:https://www.cnblogs.com/White-xzx/ 原文地址:https://arxiv.org/abs/1709.09930 Github: https://git ...