iOS sqlite 使用事务操作数据库
业务层代码:
//将解析的更新人员数据批量同步到数据库
+(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 使用事务操作数据库的更多相关文章
- sqlite:多线程操作数据库“database is locked”解决方法(二)
上一篇博客<sqlite:多线程操作数据库“database is locked”解决方法>通过注册延时函数的方法来处理数据库被锁的问题.此方法固然能解决问题,但是在多个线程向数据库写入大 ...
- iOS sqlite C语言操作
利用周六时间看了一下关于sqlite的知识.在这记录一下.看的传智播客视频 对数据的操作基本上就是增删改查: static sqlite3 *db; //声明一个数据库 @implementation ...
- JavaWeb_(Spring框架)整合Mybatis加入事务操作数据库
整合Mybatis a)导包: i.Spring:基本包.aop.aspects.jdbc.tx.test: ii.Mybatis:mybatis-3.4.6 iii.整合包:mybatis-spri ...
- android: SQLite使用 SQL 操作数据库
虽然 Android 已经给我们提供了很多非常方便的 API 用于操作数据库,不过总会有一些 人不习惯去使用这些辅助性的方法,而是更加青睐于直接使用 SQL 来操作数据库.这种人 一般都是属于 SQL ...
- iOS sqlite 的各种操作
iOS --SQL的增加.删除.查找.修改 iOS对于数据库的操作:增加.删除.查找.修改 首先需要创建一个数据库:本程序的数据库是在火狐浏览器里的插件里写的微量型数据库 火狐找查找SQLite Ma ...
- sqlite:多线程操作数据库“database is locked”解决方法
1. 使sqlite支持多线程(不确定是否非加不可,暂且加上,以备后患) 可以在编译时/启动时/运行时选择线程模式,参考:http://www.cnblogs.com/liaj/p/4015219.h ...
- [PHP] - PDO事务操作
PHP使用PDO事务操作数据库. 参考文章: http://php.ncong.com/mysql/pdo/pdo_shiwu.html 上代码: <!doctype html> < ...
- c#操作数据库的增删改查语句及DataGridView简单使用
下面是要用户名和密码连接数据库的操作: 一.定义连接字符串,用来链接SQL Server string str_con = "server=.(服务器名称一般为 . );database=W ...
- andorid SQLite数据库的增删改查 和事务操作
.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android ...
随机推荐
- VMware下centos桥接模式静态ip配置
声明:本文转载自http://blog.csdn.net/ltr15036900300/article/details/48828207,非原创. 一.配置虚拟机centos网络 备份网络文件 [ro ...
- Vue中发送ajax请求——axios使用详解
axios 基于 Promise 的 HTTP 请求客户端,可同时在浏览器和 node.js 中使用 功能特性 在浏览器中发送 XMLHttpRequests 请求 在 node.js 中发送 htt ...
- Android 6.0 API
Android 6.0 (M) 为用户和应用开发者提供了新功能.本文旨在介绍其中最值得关注的 API. 着手开发 要着手开发 Android 6.0 应用,您必须先获得 Android SDK,然后使 ...
- 在win7_64bit + ubuntu-12.04-desktop-amd64+VMware-workstation-full-10.0.1-1379776平台上安装ns-allinone-2.35
step1. ns-allinone-2.35的下载地址:http://www.isi.edu/nsnam/ns/ns-build.html#allinone step2. 在虚拟机中打开term ...
- k8s的imagePullSecrets如何生成及使用
如果公司的docker仓库(harbor),需要用户认证之后,才能拉取镜像. 那如何在k8s里生成这个secret呢? 这个secret如何还原呢? 在k8s的yaml文件里如何实现呢? 这里提供几个 ...
- 【PAT】1052 Linked List Sorting (25)(25 分)
1052 Linked List Sorting (25)(25 分) A linked list consists of a series of structures, which are not ...
- Java 分布式系统 实现session共享
当然业界已经有很多成熟的解决方案,我罗列如下: 1.服务器实现的session复制或session共享,这类型的共享session是和服务器紧密相关的,比如webSphere或JBOSS在搭建集群时候 ...
- C++智能指针,指针容器原理及简单实现(auto_ptr,scoped_ptr,ptr_vector).
目录 C++智能指针,指针容器原理及简单实现(auto_ptr,scoped_ptr,ptr_vector). auto_ptr scoped_ptr ptr_vector C++智能指针,指针容器原 ...
- Spring-Session实现Session共享实现原理以及源码解析
知其然,还要知其所以然 ! 本篇介绍Spring-Session的整个实现的原理.以及对核心的源码进行简单的介绍! 实现原理介绍 实现原理这里简单说明描述: 就是当Web服务器接收到http请求后,当 ...
- 处理javabean的JSP标签
(1) 关于javabean要求: 1,具有无参的构造函数. 2,针对每一个成员变量,因改提供相应get/set. 3,implments Serializable(实现才能对象序列化). (2) 使 ...