概述sql
1. iOS開發中對數據進行本地緩存可謂屢見不鮮,小數據咱們用plist文件或者歸檔緩存便可,即簡單又方便。但對於一些列表同樣的數據(數據量比較大)就要用到數據庫了。數據庫
2. 關於數據庫,移動開發中確定首選sqlite3。這是一款輕微型的嵌入式數據庫,經過sql語句進行「增刪改查」等數據操做。只是sqlite,裏面公佈的api都是一些純c語言的代碼,用起來繁瑣不堪,極爲痛苦。api
3. 而FMDB這個框架,就是對sqlite用oc語法進行了一層封裝,咱們到時使用數據庫時,就能夠直接用面向對象的方式進行數據操做。緩存
1.FMDatabase – 表示一個單獨的SQLite數據庫。 用來執行SQLite的命令。安全
2.FMResultSet – 表示FMDatabase執行查詢後結果集多線程
3.FMDatabaseQueue – 若是你想在多線程中執行多個查詢或更新,你應該使用該類。這是線程安全的。框架
- (BOOL)executeUpdate:(NSString*)sql, ...; //能執行插入數據、刪除數據、更新數據、建表刪表操做。參數:傳入要執行的sql語句dom
- (FMResultSet *)executeQuery:(NSString*)sql, ...; // 查詢數據時用此方法。參數:傳入要執行的sql語句。返回值:查詢後結果集atom
注:其餘的幾個方法不一一例舉,上面兩個方法用於數據庫操做,足矣!spa
使用時導入依賴庫:libsqlite3.dylib,在須要用的地方導入頭文件:"FMDB.h"
1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 4 //得到沙盒數據庫文件路徑,有這個文件直接得到,沒有會進行建立 5 NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"student.sqlite"]; 6 7 // NSLog(@"%@",path); 8 9 // 1.建立數據庫實例對象 10 self.db = [FMDatabase databaseWithPath:path]; 11 12 // 2.打開數據庫 13 if ([self.db open]) { 14 NSLog(@"數據庫打開成功"); 15 16 //建立一張學生表 17 BOOL result = [self.db executeUpdate:@"create table if not exists t_student(id integer primary key autoincrement,name text,age integer);"]; 18 if (result) { 19 NSLog(@"創表成功!"); 20 } 21 22 } 23 24 25 } 26 27 - (IBAction)insert { 28 //插入數據 29 for (int i = 0; i < 20; i++) { 30 NSString *name = [NSString stringWithFormat:@"name%d",i]; 31 32 //注:此處sql語句具體的值能夠用?替代,至關於佔位符,後面逗號隔開後,放具體的值,如此能夠防止數據寫死 33 BOOL result =[self.db executeUpdate:@"insert into t_student (name,age) values (?,?);",name,@(i + 20)]; 34 if (result) { 35 NSLog(@"插入數據成功!"); 36 } 37 38 } 39 40 } 41 42 - (IBAction)delete { 43 //刪除數據 44 45 [self.db executeUpdate:@"delete from t_student where age > ?;",@(30)]; 46 } 47 48 - (IBAction)update { 49 50 //更新數據 51 BOOL result = [self.db executeUpdate:@"update t_student set name = ?;",@"葉德雄"]; 52 53 if (result) { 54 NSLog(@"更新數據成功!"); 55 } 56 57 } 58 59 - (IBAction)select { 60 61 //查詢數據 62 //返回一個FMResultSet集合,經過索引取數據,即調用其方法[set next],開始沒有指向數據,返回no,調用一次指向下條數據,當最後跳數據指向完時,返回no,下面是經過while循環遍歷數據 63 FMResultSet *set = [self.db executeQuery:@"select *from t_student where age > ?;",@(30)]; 64 while (set.next) { 65 //經過字段名字取數據 66 int ID = [set intForColumn:@"id"]; 67 NSString *name = [set stringForColumn:@"name"]; 68 int age = [set intForColumn:@"age"]; 69 70 NSLog(@"id=%d,name=%@,age=%d",ID,name,age); 71 } 72 }
執行數據操做時,用到的核心方法是:
- (void)inDatabase:(void (^)(FMDatabase *db))block;//block裏面傳回數據庫實例FMDatabase *db,咱們用db對象進行增刪改查操做
2 #import "FMDB.h" 3 4 @interface ViewController () 5 6 @property(nonatomic,strong)FMDatabaseQueue *queue; 7 8 @end 9 10 @implementation ViewController 11 12 - (void)viewDidLoad { 13 14 [super viewDidLoad]; 15 16 //使用數據庫隊列操做數據,fmdb線程是不安全的,建議使用FMDatabaseQueue 17 NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"sqlite.data"]; 18 //裏面已經建立了FMDdataBase實例,數據庫實例 19 self.queue = [FMDatabaseQueue databaseQueueWithPath:path]; 20 21 //經過block,拿到FMDatabase *db 22 [self.queue inDatabase:^(FMDatabase *db) { 23 //創表 24 BOOL result = [db executeUpdate:@"create table if not exists t_student(id integer primary key autoincrement,name text,age integer);"]; 25 if (result) { 26 NSLog(@"創表成功"); 27 } 28 }]; 29 30 } 31 32 - (IBAction)insert 33 34 { //經過block,拿到FMDatabase *db 35 [self.queue inDatabase:^(FMDatabase *db) { 36 for (int i = 0; i<40; i++) { 37 NSString *name = [NSString stringWithFormat:@"rose-%d", arc4random() % 1000]; 38 NSNumber *age = @(arc4random() % 100 + 1); 39 [db executeUpdate:@"insert into t_student (name, age) values (?, ?);", name, age]; 40 } 41 }]; 42 } 43 44 - (IBAction)update 45 { 46 [self.queue inDatabase:^(FMDatabase *db) { 47 51 [db executeUpdate:@"update t_student set age = ? where name = ?;", @20, @"jack"]; 52 [db executeUpdate:@"update t_student set age = ? where name = ?;", @20, @"jack"]; 53 54 55 - (IBAction)delete 71 {73 [db executeUpdate:@"update t_student set age = ? where name = ?;", @20, @"jack"]; 74 [db executeUpdate:@"update t_student set age = ? where name = ?;", @20, @"jack"]; 75 76} 82 83 - (IBAction)query 84 { 85 [self.queue inDatabase:^(FMDatabase *db) { 86 // 1.查詢數據 87 FMResultSet *rs = [db executeQuery:@"select * from t_student where age > ?;", @50]; 88 89 // 2.遍歷結果集 90 while (rs.next) { 91 int ID = [rs intForColumn:@"id"]; 92 NSString *name = [rs stringForColumn:@"name"]; 93 int age = [rs intForColumn:@"age"]; 94 95 NSLog(@"%d %@ %d", ID, name, age); 96 } 97 }]; 98 }