iOS开发数据库篇—FMDB数据库队列
一、代码示例
1.需要先导入FMDB框架和头文件,由于该框架依赖于libsqlite库,所以还应该导入该库。
2.代码如下:
1 // 2 // YYViewController.m 3 // 05-FMDB数据库队列 4 // 5 // Created by apple on 14-7-28. 6 // Copyright (c) 2014年 wendingding. All rights reserved. 7 // 8 9 #import "YYViewController.h" 10 #import "FMDB.h" 11 12 @interface YYViewController () 13 @property(nonatomic,strong)FMDatabaseQueue *queue; 14 @end 15 16 @implementation YYViewController 17 18 - (void)viewDidLoad 19 { 20 [super viewDidLoad]; 21 22 //1.获得数据库文件的路径 23 NSString *doc=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 24 NSString *fileName=[doc stringByAppendingPathComponent:@"person.sqlite"]; 25 26 //2.获得数据库队列 27 FMDatabaseQueue *queue=[FMDatabaseQueue databaseQueueWithPath:fileName]; 28 // FMDatabase *db=[FMDatabase databaseWithPath:fileName]; 29 30 //3.打开数据库 31 [queue inDatabase:^(FMDatabase *db) { 32 BOOL result=[db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_person (id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, age integer NOT NULL);"]; 33 if (result) { 34 NSLog(@"创表成功"); 35 }else 36 { 37 NSLog(@"创表失败"); 38 } 39 }]; 40 self.queue=queue; 41 42 } 43 44 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 45 { 46 //插入数据 47 // [self.queue inDatabase:^(FMDatabase *db) { 48 // [db executeUpdate:@"INSERT INTO t_person (name, age) VALUES (?, ?);",@"wendingding", @22]; 49 // }]; 50 51 //查询数据 52 [self.queue inDatabase:^(FMDatabase *db) { 53 // 1.执行查询语句 54 FMResultSet *resultSet = [db executeQuery:@"SELECT * FROM t_person"]; 55 56 // 2.遍历结果 57 while ([resultSet next]) { 58 int ID = [resultSet intForColumn:@"id"]; 59 NSString *name = [resultSet stringForColumn:@"name"]; 60 int age = [resultSet intForColumn:@"age"]; 61 NSLog(@"%d %@ %d", ID, name, age); 62 } 63 }]; 64 65 } 66 67 @end
先插入数据,之后查询结果,打印如下:
3.代码说明
1 //插入数据 2 [self.queue inDatabase:^(FMDatabase *db) { 3 [db beginTransaction]; 4 [db executeUpdate:@"INSERT INTO t_person (name, age) VALUES (?, ?);",@"wendingding", @22]; 5 [db executeUpdate:@"INSERT INTO t_person (name, age) VALUES (?, ?);",@"wendingding", @23]; 6 [db executeUpdate:@"INSERT INTO t_person (name, age) VALUES (?, ?);",@"wendingding", @24]; 7 [db executeUpdate:@"INSERT INTO t_person (name, age) VALUES (?, ?);",@"wendingding", @25]; 8 [db commit]; 9 }];
如果中途出现问题,那么会自动回滚,也可以选择手动回滚。
1 //插入数据 2 [self.queue inDatabase:^(FMDatabase *db) { 3 [db beginTransaction]; 4 [db executeUpdate:@"INSERT INTO t_person (name, age) VALUES (?, ?);",@"wendingding", @22]; 5 [db executeUpdate:@"INSERT INTO t_person (name, age) VALUES (?, ?);",@"wendingding", @23]; 6 [db executeUpdate:@"INSERT INTO t_person (name, age) VALUES (?, ?);",@"wendingding", @24]; 7 [db rollback]; 8 [db executeUpdate:@"INSERT INTO t_person (name, age) VALUES (?, ?);",@"wendingding", @25]; 9 [db commit]; 10 }];
上面的代码。前三条插入语句是作废的。
事务处理的另一种方式:
1 [self.queue inTransaction:^(FMDatabase *db, BOOL *rollback) { 2 [db executeUpdate:@"INSERT INTO t_person (name, age) VALUES (?, ?);",@"wendingding", @22]; 3 [db executeUpdate:@"INSERT INTO t_person (name, age) VALUES (?, ?);",@"wendingding", @23]; 4 [db executeUpdate:@"INSERT INTO t_person (name, age) VALUES (?, ?);",@"wendingding", @24]; 5 }];
说明:先开事务,再开始事务,之后执行block中的代码段,最后提交事务。
iOS开发数据库篇—FMDB数据库队列,布布扣,bubuko.com
原文:http://www.cnblogs.com/wendingding/p/3873874.html