iOS中原生的SQLite
API在進行數據存儲的時候,須要使用C語言中的函數,操做比較麻煩。因而,就出現了一系列將SQLite API進行封裝的庫,例如FMDB
、PlausibleDatabase
、sqlitepersistentobjects
等。ios
FMDB是一款簡潔、易用的封裝庫。所以,在這裏推薦使用第三方框架FMDB,它是對libsqlite3框架的封裝,用起來的步驟與SQLite使用相似,而且它對於多線程的併發操做進行了處理,因此是線程安全的。git
SQLite
的C語言API,使用起來更加的方便;FMDatabase
FMDatabase
對象就表明一個單獨的SQLite
數據庫,用來執行SQL
語句FMResultSet
FMDatabase
執行查詢後的結果集FMDatabaseQueue
libsqlite3.0
框架,導入頭文件FMDatabase.h
step.pnggithub
數據庫建立sql
建立FMDatabase對象時參數爲SQLite數據庫文件路徑,該路徑能夠是如下三種方式之一數據庫
NULL。將建立一個內在數據庫,一樣的,當FMDatabase鏈接關閉時,數據將會被銷燬數組
本文中使用的測試模型類.h瀏覽器
student.png安全
數據庫使用FMDB
框架代碼操做多線程
使用FMDataBase
類創建數據庫併發
//1.得到數據庫文件的路徑 NSString *doc =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) lastObject]; NSString *fileName = [doc stringByAppendingPathComponent:@「student.sqlite」]; //2.得到數據庫 FMDatabase *db = [FMDatabase databaseWithPath:fileName]; //3.使用以下語句,若是打開失敗,多是權限不足或者資源不足。一般打開完操做操做後,須要調用 close 方法來關閉數據庫。在和數據庫交互 以前,數據庫必須是打開的。若是資源或權限不足沒法打開或建立數據庫,都會致使打開失敗。 if ([db open]) { //4.創表 BOOL result = [db executeUpdate:@「CREATE TABLE IF NOT EXISTS t_student (id integer PRIMARY KEY AUTOINCREM ENT, name text NOT NULL, age integer NOT NULL);」]; if (result) { NSLog(@「建立表成功」); } }
2.png
sql.png
使用FMDataBase
類執行數據庫命令SQL
一切不是SELECT命令的命令都視爲更新。這包括 CREAT,UPDATE,INSERT,ALTER,BEGIN,COMMIT,DETACH,DELETE,DROP,END,EXPLAIN,VACUUM,REPLACE等。
簡單來講,只要不是以SELECT開頭的命令都是更新命令。
執行更新返回一個BOOL值。YES表示 執行成功,不然表示有錯誤。你能夠調用 -lastErrorMessage 和 -lastErrorCode方法來獲得更多信息。
使用FMDataBase
類執行數據庫插入命令SQLinsert into
int age = 42; //1.executeUpdate:不肯定的參數用?來佔位(後面參數必須是oc對象,;表明語句結束) [self.db executeUpdate:@「INSERT INTO t_student (name, age) VALUES (?,?);」,name,@(age)]; //2.executeUpdateWithForamat:不肯定的參數用%@,%d等來佔位 (參數爲原始數據類型,執行語句不區分大小寫) [self.db executeUpdateWithForamat:@「insert into t_student (name,age) values (%@,%i);」,name,age]; //3.參數是數組的使用方式 [self.db executeUpdate:@「INSERT INTO t_student(name,age) VALUES (?,?);」withArgumentsInArray:@[name,@(age )]];
使用FMDataBase
類執行數據庫刪除命令SQLdelete
//1.不肯定的參數用?來佔位 (後面參數必須是oc對象,須要將int包裝成OC對象) int idNum = 101; [self.db executeUpdate:@「delete from t_student where id = ?;」,@(idNum)]; //2.不肯定的參數用%@,%d等來佔位 [self.db executeUpdateWithFormat:@「delete from t_student where name = %@;」,@「apple_name」];
使用FMDataBase
類執行數據庫修改命令SQLupdate
//修改學生的名字 [self.db executeUpdate:@「update t_student set name = ? where name = ?」,newName,oldName];
使用FMDataBase
類執行數據庫查詢命令SQLselect ... from
FMResultSet
獲取不一樣數據格式的方法
使用FMResultSet
獲取查詢語句結果
//查詢整個表 FMResultSet *resultSet = [self.db execute Query:@「select * from t_student;」]; //根據條件查詢 FMResultSet *resultSet = [self.db executeQuery:@「select * from t_student where id<?;」@(14)]; //遍歷結果集合 while ([resultSet next]) { int idNum = [resultSet intForColumn:@「id」]; NSString *name = [resultSet objectForColumn:@「name」]; int age = [resultSet intForColumn:@「age」]; }
使用FMDataBase
類執行數據庫銷燬命令SQLdrop ...
//若是表格存在 則銷燬 [self.db executeUpadate:@「drop table if exists t_student;」];
使用FMDatabaseQueue
類實現多線程操做
在多個線程中同時使用一個FMDatabase實例是不明智的。如今你能夠爲每 個線程建立一個FMDatabase對象,不要讓多個線程分享同一個實例,他無 法在多個線程中同事使用。不然程序會時不時崩潰或者報告異常。因此,不要 初始化FMDatabase對象,而後在多個線程中使用。這時候,咱們就須要使 用FMDatabaseQueue來建立隊列執行事務。
//1.建立隊列 FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:aPath]; __block BOOL whoopsSomethingWrongHappened = true; //2.把任務包裝到事務裏 [queue inTransaction:^(FMDatabase *db, BOOL *rollback) { whoopsSomethingWrongHappened &= [db executeUpdate:@「INSERT INTO myTable VALUES (?)」, [NSNumber numberWith:1]]; whoopsSomethingWrongHappened &= [db executeUpdata:@「INSERT INTO myTable VALUES (?)」, [NSNumber numberWithInt:2]]; whoopsSomethingWrongHappened &= [db executeUpdata:@「INSERT INTO myTable VALUES (?)」[NSNumber numberWithInt:3]]; //若是有錯誤 返回 if (!whoopsSomethingWrongHappened) { *rollback = YES; return; } }];
好了,到此爲止,相信你已經可以使用FMDB進行數據持久化了,它的好與壞 只有在不斷地使用過程當中才能發現瞭解。因此,但願你們學會了之後仍是要多 寫多練多使用。另外,誠心但願你們多提寶貴意見,或者溝通一些好的想法。^^