iPhone資料儲存中關於Sqlite 3使用第三方架構FMDB是本文要介紹的內容,主要講述在iPhone上是採用sqlite進行資料儲存是我一種比較習慣性的做法。一般在其他平台也比較習慣用sqlite,比如android。
而iphone上有一些封裝好的第三方架構提供使用,更節省了許多時間。如:Sqlitepersistentobjects ,FMDB 。今天尋找了這個兩個架構,感覺FMDB的風格更符合我的使用,其實兩者是各有優點的,只是看個人喜好而已。以下是FMDB的一些基本使用,FMDB架構其實只是一層很薄的封裝,主要的類也就兩個:FMDatabase和FMResultSet ;
其中的FMResultSet對象讓我想起了android中sqlite的cursor集合啊。
FMDB的github地址是,https://github.com/ccgus/fmdb。
1、首先得執行個體化一個FMDatabase對象,這跟 Sqlitepersistentobjects 派生一個子類進行操作是不同。接著開啟一個資料庫如果沒有會建立一個資料庫)
- //paths: ios下Document路徑,Document為ios中可讀寫的檔案夾
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *documentDirectory = [paths objectAtIndex:0];
- //dbPath: 資料庫路徑,在Document中。
- NSString *dbPath = [documentDirectory stringByAppendingPathComponent:@"Test.db"];
- //建立資料庫執行個體 db 這裡說明下:如果路徑中不存在"Test.db"的檔案,sqlite會自動建立"Test.db"
- FMDatabase *db= [FMDatabase databaseWithPath:dbPath] ;
- if (![db open]) {
- NSLog(@"Could not open db.");
- return ;
- }
- //paths: ios下Document路徑,Document為ios中可讀寫的檔案夾
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *documentDirectory = [paths objectAtIndex:0];
- //dbPath: 資料庫路徑,在Document中。
- NSString *dbPath = [documentDirectory stringByAppendingPathComponent:@"Test.db"];
- //建立資料庫執行個體 db 這裡說明下:如果路徑中不存在"Test.db"的檔案,sqlite會自動建立"Test.db"
- FMDatabase *db= [FMDatabase databaseWithPath:dbPath] ;
- if (![db open]) {
- NSLog(@"Could not open db.");
- return ;
- }
接下來,我們可以通過這個資料庫物件進行操作了。操作主要是update和queries。
首先是建立表。
- //建立一個名為User的表,有兩個欄位分別為string類型的Name,integer類型的 Age
- [db executeUpdate:@"CREATE TABLE User (Name text,Age integer)"];
- //建立一個名為User的表,有兩個欄位分別為string類型的Name,integer類型的 Age
- [db executeUpdate:@"CREATE TABLE User (Name text,Age integer)"];
這樣我們就有了一張表了。接下我們對錶進行操作。插入資料!注意插入的資料使用了萬用字元,這跟iphone直接使用sqlite借口的綁定變數是一樣的,後面的萬用字元匹配的資料。
- //插入資料使用OC中的類型 text對應為NSString integer對應為NSNumber的整形
- [db executeUpdate:@"INSERT INTO User (Name,Age) VALUES (?,?)",@"老婆",[NSNumber numberWithInt:20]]
- //插入資料使用OC中的類型 text對應為NSString integer對應為NSNumber的整形
- [db executeUpdate:@"INSERT INTO User (Name,Age) VALUES (?,?)",@"老婆",[NSNumber numberWithInt:20]]
接下來是更新資料。
- //更新資料 將“老婆”更改為“寶貝”
- [db executeUpdate:@"UPDATE User SET Name = ? WHERE Name = ? ",@"老婆",@"寶貝"];
- //更新資料 將“老婆”更改為“寶貝”
- [db executeUpdate:@"UPDATE User SET Name = ? WHERE Name = ? ",@"老婆",@"寶貝"];
再接下來,就是刪除資料啦。
- //刪除資料
- [db executeUpdate:@"DELETE FROM User WHERE Name = ?",@"老婆"];
- //刪除資料
- [db executeUpdate:@"DELETE FROM User WHERE Name = ?",@"老婆"];
update的基本操作就這幾個,接下來是queries!
- //返回資料庫中第一條滿足條件的結果
- NSString *aa=[db stringForQuery:@"SELECT Name FROM User WHERE Age = ?",@"20"];
- //返回資料庫中第一條滿足條件的結果
- NSString *aa=[db stringForQuery:@"SELECT Name FROM User WHERE Age = ?",@"20"];
這樣我們就查詢返回了一條資料,那當我們想要查詢放返回多條資料怎麼辦呢?不用愁,之前我就提到了FMDB中的另外一個主要的類,FMResultSet,這是一個結果集!返回多條資料時FMDB會將資料放在這個結果集中,然後我們在對這個結果集進行查詢操作!很簡單。
- FMResultSet *rs=[db executeQuery:@"SELECT * FROM User"];
- rs=[db executeQuery:@"SELECT * FROM User WHERE Age = ?",@"20"];
- while ([rs next]){
- NSLog(@"%@ %@",[rs stringForColumn:@"Name"],[rs stringForColumn:@"Age"]);
- }
- FMResultSet *rs=[db executeQuery:@"SELECT * FROM User"];
- rs=[db executeQuery:@"SELECT * FROM User WHERE Age = ?",@"20"];
- while ([rs next]){
- NSLog(@"%@ %@",[rs stringForColumn:@"Name"],[rs stringForColumn:@"Age"]);
- }
更多的 FMResultSet方法有:
- intForColumn:
- longForColumn:
- longLongIntForColumn:
- boolForColumn:
- doubleForColumn:
- stringForColumn:
- dateForColumn:
- dataForColumn:
- dataNoCopyForColumn:
- UTF8StringForColumnIndex:
- objectForColumn:
具體查看一下類就行了! 好了,對於FMDB的使用就這樣,是不是很簡單呢,其實這個些封裝sqlite的架構都是萬變不離其宗的,只要你掌握了sql就行了!
小結:iPhone資料儲存中關於Sqlite 3使用第三方架構FMDB的內容介紹完了,希望本文對你有所協助!