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的问题在本文中解答一下. 本文旨在指出,在 ...
随机推荐
- WPF Visibility属性用法
WPF Visibility属性用法 Visible 元素在窗体中正常显示 Collaspsed 元素不显示,也不占用空间 Hidden 元素不显示,但是任然为它保留空间
- 设置win7资源管理器启动时的默认位置-windows-操作系统-网页教学网
设置win7资源管理器启动时的默认位置-windows-操作系统-网页教学网 如何设置win7资源管理器启动时的默认位置?我不太习惯 Win 7 的资源管理器默认总是打开库,我还是喜欢资源管理器打开树 ...
- 使用struts2进行文件下载以及下载权限控制的例子
本测试有两个模块,一个是文件上上传,一个是文件下载,文件下载的时候会检查是否足有权限,如果没有,就会转发到登录页面,如果有权限,就会直接启动下载程序,给浏览器一个输出流. 下面直接上我的代码: 登录表 ...
- “在注释中遇到意外的文件结束”--记一个令人崩溃的bug
下午写程序,写的好好的,突然报错"在注释中遇到意外的文件结束". 下面是官方给出的错误原因是缺少注释终结器 (* /). // C1071.cpp int main() { } / ...
- 获取显示屏的个数和分辨率 --- 通过使用OpenGL的GLFW库
获取显示屏的个数和分辨率 - 通过使用OpenGL的GLFW库 程序 #include <iostream> // GLFW #include <GLFW/glfw3.h> i ...
- PLSQL Developer 直接用ip访问指定数据库
- HTML5秘籍(第2版) 中文pdf扫描版
HTML5秘籍(第2版)共包括四个部分,共13章.第一部分介绍了HTML5的发展历程,用语义元素构造网页,编写更有意义的标记,以及构建更好的Web表单.第二部分介绍了HTML5中的音频与视频.CS ...
- LeetCode: 455 Assign Cookies(easy)
题目: Assume you are an awesome parent and want to give your children some cookies. But, you should gi ...
- (linux)修改MySQL密码方法
1,在/etc/my.cnf末尾 加入skip-grant-tables,保存,跳过身份验证. 2,重启MySql,使刚才修改的配置生效. 3,终端输入mysql,然后再输入use mysql; 4 ...
- Python Day24
AJAX 对于WEB应用程序:用户浏览器发送请求,服务器接收并处理请求,然后返回结果,往往返回就是字符串(HTML),浏览器将字符串(HTML)渲染并显示浏览器上. 1.传统的Web应用 一个简单操作 ...