1、代碼示例sql
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
先插入數據,以後查詢結果,打印以下:app
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 }];
若是中途出現問題,那麼會自動回滾,也能夠選擇手動回滾。atom
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 }];
上面的代碼。前三條插入語句是做廢的。spa
事務處理的另外一種方式:線程
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中的代碼段,最後提交事務。3d