//  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的更多相关文章

  1. Entity Framework 的事务 DbTransaction

    事务代码实现如下: public static void Transaction() { myitEntities entity = null; DbTransaction tran = null; ...

  2. SQLCommand命令、DbTransaction事务

    一.SqlDataReader SqlConnection conn = new SqlConnection("server=10.126.64.11;user=it_oper;pwd=IT ...

  3. DataAccess通用数据库访问类,简单易用,功能强悍

    以下是我编写的DataAccess通用数据库访问类,简单易用,支持:内联式创建多个参数.支持多事务提交.支持参数复用.支持更换数据库类型,希望能帮到大家,若需支持查出来后转换成实体,可以自行扩展dat ...

  4. 兼容SQLSERVER、Oracle、MYSQL、SQLITE的超级DBHelper

    本示例代码的关键是利用.net库自带的DbProviderFactory来生产数据库操作对象. 从下图中,可以看到其的多个核心方法,这些方法将在我们的超级DBHelper中使用. 仔细研究,你会发现每 ...

  5. C#/ASP.NET完善的DBHelper,配套Model生成器

    支持Oracle.MSSQL.MySQL.SQLite四种数据库,支持事务,支持对象关系映射:已在多个项目中实际使用. 没有语法糖,学习成本几乎为0,拿来即用. DBHelper类完整代码: usin ...

  6. 基于Metronic的Bootstrap开发框架经验总结(13)--页面链接收藏夹功能的实现2(利用Sortable进行拖动排序)

    在上篇随笔<基于Metronic的Bootstrap开发框架经验总结(12)--页面链接收藏夹功能的实现>上,我介绍了链接收藏夹功能的实现,以及对收藏记录的排序处理.该篇随笔主要使用功能按 ...

  7. C#开发微信门户及应用(10)--在管理系统中同步微信用户分组信息

    在前面几篇文章中,逐步从原有微信的API封装的基础上过渡到微信应用平台管理系统里面,逐步介绍管理系统中的微信数据的界面设计,以及相关的处理操作过程的逻辑和代码,希望从更高一个层次,向大家介绍微信的应用 ...

  8. 搭建一套自己实用的.net架构(3)【ORM-Dapper+DapperExtensions】

    现在成熟的ORM比比皆是,这里只介绍Dapper的使用(最起码我在使用它,已经运用到项目中,小伙伴们反馈还可以). 优点: 1.开源.轻量.小巧.上手容易. 2.支持的数据库还蛮多的, Mysql,S ...

  9. .NET Core中ADO.NET SqlClient的使用与常见问题

    一.简介 在很多要求性能的项目中,我们都要使用传统的ADO.NET的方式来完成我们日常的工作:目前有一些网友问有关于.NET Core操作SQL Server的问题在本文中解答一下. 本文旨在指出,在 ...

随机推荐

  1. ubuntu下sourceinsight的安装

    转载自blog.csdn.net/zzobin/article/details/7376616 1. 安装wine 详看:http://wiki.ubuntu.org.cn/Wine sudo apt ...

  2. SQL笔记:中级篇

    1.LIMIT 查询前多少条数据 例如:查询user表前三条数据 SELECT * FROM  user LIMIT 3 ORACLE:  SELECT name FROM user WHERE RO ...

  3. adnroid 启动是没有标题栏

    <activity android:name=".MainActivity" android:theme="@android:style/Theme.Light.N ...

  4. ProtoBuf练习(二)

    重复数据类型 protobuf语言的重复字段类型相当于C++的std::list数据类型 工程目录结构 $ ls proto/ TServer.proto TSession.proto proto文件 ...

  5. [WIP]php 基本语法

    创建: 2019/06/14 https://www.php.net/manual/zh/langref.php php标记   当解析一个文件时,PHP 会寻找起始和结束标记,也就是 <?ph ...

  6. SQL Server远程调试失败

    前言 刚刚打开SQL Server 2008,想要新建一个数据库.发现出现了一个问题,这个问题由于之前没有遇到过,所以这次拿出来记录一些解决方式. 内容 出现上面这个错误的原因可能是由于咱们在装VS2 ...

  7. bzoj1010 玩具装箱

    玩具装箱 P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为1...N的N件玩具, ...

  8. 洛谷P2580(trie)

    第一行一个整数 n,表示班上人数.接下来 n 行,每行一个字符串表示其名字(互不相同,且只含小写字母,长度不超过 50).第 n+2 行一个整数 m,表示教练报的名字.接下来 m 行,每行一个字符串表 ...

  9. AT2045 Salvage Robots

    传送门 这个题只要想到移动机器人和移动出口是等价的就好做了 考虑设\(f[i][j][k][t]\)为最远向左移动\(i\),向右移动\(j\),向上移动\(k\),向下移动\(t\),这个矩形内最多 ...

  10. 洛谷P2025 脑力大人之监听电话

    题目描述 话说埃菲尔铁塔小区的房子只有一栋,且只有一层,其中每一家都装有一个监听器,具体地,如果编号为第i家的人给编号第\(j\)家的人打了电话,\(i \leq j\),当然,也会有些人无聊地自己给 ...