业务层代码:

//将解析的更新人员数据批量同步到数据库
+(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. PHP 利用redis 做统计缓存mysql的压力

    <?php header("Content-Type:text/html;charset=utf-8"); include 'lib/mysql.class.php'; $m ...

  2. Angular和Vue.js

    Angular和Vue.js Vue.js 是开源的 JavaScript 框架,能够帮助开发者构建出美观的 Web 界面.当和其它网络工具配合使用时,Vue.js 的优秀功能会得到大大加强.如今,已 ...

  3. ClouderaManager中Event Server报No such file or directory

    错误日志如下: 2015-06-24 06:13:10,176 ERROR com.cloudera.cmf.eventcatcher.server.EventCatcherService: Erro ...

  4. 008 jquery过滤选择器-----------(子元素过滤选择器)

    1.介紹 2.程序 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...

  5. Android中加载事件的方式

    Android中加载事件的方式 通过内部类的方式实现 通过外部类的方式实现 通过属性的方式实现 通过自身实现接口的方式实现 通过内部类的方式实现 Demo btn_Login.setOnClickLi ...

  6. codevs 水过 骑马修栅栏

    [问题描述] 农民John每年有很多栅栏要修理.他总是骑着马穿过每一个栅栏并修复它破损的地方. John是一个与其他农民一样懒的人.他讨厌骑马,因此从来不两次经过一个一个栅栏.你必须编一个程序,读入栅 ...

  7. JZYZOJ 2041 快速数论变换 NTT 多项式

    http://172.20.6.3/Problem_Show.asp?id=2041 https://blog.csdn.net/ggn_2015/article/details/68922404 代 ...

  8. [POI2013]Taksówki

    [POI2013]Taksówki 题目大意: ABC三地在同一条直线上,AC相距\(m(m\le10^{18})\)米,AB相距\(d\),B在AC之间.总共有\(n(n\le5\times10^5 ...

  9. Xtreme9.0 - Mr. Pippo's Pizza 数学

    Mr. Pippo's Pizza 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/mr-pipp ...

  10. 2013-2014 ACM-ICPC, NEERC, Southern Subregional Contest Problem H. Password Service dp

    Problem H. Password Service 题目连接: http://www.codeforces.com/gym/100253 Description Startups are here ...