DBTransaction
// Created by 张国锋 on 15-7-23.
// Copyright (c) 2014年 张国锋. All rights reserved.
// #import "AppDelegate.h"
#import "RootViewController.h" @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
RootViewController *root = [[RootViewController alloc] init];
self.window.rootViewController =root;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
} //
// RootViewController.m
// DBTransaction -FMDB
//
// Created by 张国锋 on 15-7-23.
// Copyright (c) 2014年 张国锋. All rights reserved.
// #import "RootViewController.h"
#import "FMDatabase.h"
//需要导入libsqlite3.dylib系统库
@interface RootViewController ()
{
FMDatabase *_dataBase;
}
@end @implementation RootViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
} - (void)viewDidLoad
{
[super viewDidLoad];
NSString *dbPath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/test.db"];
//初始化
_dataBase = [[FMDatabase alloc] initWithPath:dbPath];
if ([_dataBase open]) {
//创建表
NSString *createSql = @"create table if not exists student(id integer,name varchar(256))";
if (![_dataBase executeUpdate:createSql]) {
NSLog(@"create error:%@",_dataBase.lastErrorMessage);
}
}
//NSDate 时间类
NSDate *date1 = [NSDate date];//获取系统当前时间
[self insertDataWithCount:1000 isUseTransaction:YES];
NSDate *date2 = [NSDate date];
//取到时间的差值 (timeIntervalSinceDate 两个时间的差值,单位是秒)
//NSTimeInterval 时间差变量,秒
NSTimeInterval time = [date2 timeIntervalSinceDate:date1];
NSLog(@"time:%f",time); // Do any additional setup after loading the view.
}
//插入批量数据,是否手动启用事务
- (void)insertDataWithCount:(NSInteger)count isUseTransaction:(BOOL)isUse{
if (isUse) {
//手动启用事务
BOOL isError = NO;
@try {
//写可能出现异常的代码
[_dataBase beginTransaction];//手动开启一个事务
for (int i=0; i<count; i++) {
NSString *idStr =[NSString stringWithFormat:@"%d",i];
NSString *stName = [NSString stringWithFormat:@"student%d",i];
NSString *insertSql = @"insert into student(id,name) values(?,?)";
if (![_dataBase executeUpdate:insertSql,idStr,stName]) {
NSLog(@"insert error:%@",_dataBase.lastErrorMessage);
}
}
}
@catch (NSException *exception) {
//捕获到异常
NSLog(@"error:%@",exception.reason);
isError = YES;
[_dataBase rollback];//回滚,回到最初的状态
}
@finally {
//无论有没有异常,代码都会执行到此处
if (isError==NO) {
[_dataBase commit];//提交事务,让批量操作生效 }
}
}else{
//常规操作
for (int i=0; i<count; i++) {
NSString *idStr =[NSString stringWithFormat:@"%d",i];
NSString *stName = [NSString stringWithFormat:@"student%d",i];
NSString *insertSql = @"insert into student(id,name) values(?,?)";
if (![_dataBase executeUpdate:insertSql,idStr,stName]) {
NSLog(@"insert error:%@",_dataBase.lastErrorMessage);
}
}
}
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
//清除缓存
[[SDImageCache sharedImageCache]getSize]
{
[[SDImageCache sharedImageCache] clearDisk];
[[SDImageCache sharedImageCache] clearMemory];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
//清除cookies
NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies]) {
[storage deleteCookie:cookie];
}
}
+ (void)cancelWebCache
{
[[NSURLCache sharedURLCache] removeAllCachedResponses];
//清除cookies
NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies]) {
[storage deleteCookie:cookie];
}
}
DBTransaction的更多相关文章
- Entity Framework 的事务 DbTransaction
事务代码实现如下: public static void Transaction() { myitEntities entity = null; DbTransaction tran = null; ...
- SQLCommand命令、DbTransaction事务
一.SqlDataReader SqlConnection conn = new SqlConnection("server=10.126.64.11;user=it_oper;pwd=IT ...
- DataAccess通用数据库访问类,简单易用,功能强悍
以下是我编写的DataAccess通用数据库访问类,简单易用,支持:内联式创建多个参数.支持多事务提交.支持参数复用.支持更换数据库类型,希望能帮到大家,若需支持查出来后转换成实体,可以自行扩展dat ...
- 兼容SQLSERVER、Oracle、MYSQL、SQLITE的超级DBHelper
本示例代码的关键是利用.net库自带的DbProviderFactory来生产数据库操作对象. 从下图中,可以看到其的多个核心方法,这些方法将在我们的超级DBHelper中使用. 仔细研究,你会发现每 ...
- C#/ASP.NET完善的DBHelper,配套Model生成器
支持Oracle.MSSQL.MySQL.SQLite四种数据库,支持事务,支持对象关系映射:已在多个项目中实际使用. 没有语法糖,学习成本几乎为0,拿来即用. DBHelper类完整代码: usin ...
- 基于Metronic的Bootstrap开发框架经验总结(13)--页面链接收藏夹功能的实现2(利用Sortable进行拖动排序)
在上篇随笔<基于Metronic的Bootstrap开发框架经验总结(12)--页面链接收藏夹功能的实现>上,我介绍了链接收藏夹功能的实现,以及对收藏记录的排序处理.该篇随笔主要使用功能按 ...
- C#开发微信门户及应用(10)--在管理系统中同步微信用户分组信息
在前面几篇文章中,逐步从原有微信的API封装的基础上过渡到微信应用平台管理系统里面,逐步介绍管理系统中的微信数据的界面设计,以及相关的处理操作过程的逻辑和代码,希望从更高一个层次,向大家介绍微信的应用 ...
- 搭建一套自己实用的.net架构(3)【ORM-Dapper+DapperExtensions】
现在成熟的ORM比比皆是,这里只介绍Dapper的使用(最起码我在使用它,已经运用到项目中,小伙伴们反馈还可以). 优点: 1.开源.轻量.小巧.上手容易. 2.支持的数据库还蛮多的, Mysql,S ...
- .NET Core中ADO.NET SqlClient的使用与常见问题
一.简介 在很多要求性能的项目中,我们都要使用传统的ADO.NET的方式来完成我们日常的工作:目前有一些网友问有关于.NET Core操作SQL Server的问题在本文中解答一下. 本文旨在指出,在 ...
随机推荐
- LAMP 1.4 PHP编译安装
1.下载 ...
- Java探索之旅(18)——多线程(2)
1 线程协调 目的对各线程进行控制,保证各自执行的任务有条不紊且有序并行计算.尤其是在共享资源或者数据情况下. 1.1 易变volatile cache技术虽然提高了访问数据的效率,但是有可能导致主存 ...
- Dialog 基本使用
1 : 效果图 btnGeneral.setOnClickListener(new View.OnClickListener() { @Override public void onClick( ...
- C++中对象的常引用
直接传递对象名 用对象名做函数参数时,在函数调用时将建立一个新的对象,它是形参对象的拷贝. ================下面给出一个直接传递对象名的例子程序1.1================= ...
- Add lombok to IntelliJ IDEA
Lombok study link: https://www.jianshu.com/p/365ea41b3573 Add below dependency code to pom.xml <d ...
- 【机器学习】关联规则分析(一):Apriori
一.Apriori原理 Apriori是关联分析中较早的一种方法,主要用来挖掘那些频繁项集合,其思想是: 1.如果一个项目集合不是频繁集合,那么任何包含它的项目(超集)也一定不是频繁集. 2.如果一个 ...
- Linux常用知识点汇总
常用命令 1.ls 列出目录下的所有文件及文件夹 2.pwd 打印出当前所在目录 3. ./ 执行 .sh 文件命令 4.ip addr 查看ip地址 5.sudo service network ...
- 自签名配置HTTPS
基于AFN3.0 1.将后台提供的.cer文件文件保存至本地 2.在封装的网络请求工具类中为AFN的AFSecurityPolicy属性赋值 -(AFSecurityPolicy *)customSe ...
- [CentOS7] systemd
声明:本文主要总结自:鸟哥的Linux私房菜-第十七章.認識系統服務 (daemons),如有侵权,请通知博主 查看当前系统设定的服务启动脚本的类型:ls /usr/lib/systemd/syste ...
- 前端编码规范 -- css篇
合理的避免使用ID 一般情况下ID不应该被应用于样式. ID的样式不能被复用并且每个页面中你只能使用一次ID. 使用ID唯一有效的是确定网页或整个站点中的位置. 尽管如此,你应该始终考虑使用class ...