iPhone資料存放區中關於Sqlite3使用第三方架構FMDB

來源:互聯網
上載者:User

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 派生一個子類進行操作是不同。接著開啟一個資料庫如果沒有會建立一個資料庫)

 
  1. //paths: ios下Document路徑,Document為ios中可讀寫的檔案夾     
  2. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    
  3. NSString *documentDirectory = [paths objectAtIndex:0];    
  4. //dbPath: 資料庫路徑,在Document中。     
  5. NSString *dbPath = [documentDirectory stringByAppendingPathComponent:@"Test.db"];    
  6. //建立資料庫執行個體 db  這裡說明下:如果路徑中不存在"Test.db"的檔案,sqlite會自動建立"Test.db"     
  7. FMDatabase *db= [FMDatabase databaseWithPath:dbPath] ;    
  8. if (![db open]) {    
  9. NSLog(@"Could not open db.");    
  10. return ;    
  11. }    
  12. //paths: ios下Document路徑,Document為ios中可讀寫的檔案夾  
  13. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  14. NSString *documentDirectory = [paths objectAtIndex:0];  
  15. //dbPath: 資料庫路徑,在Document中。  
  16. NSString *dbPath = [documentDirectory stringByAppendingPathComponent:@"Test.db"];  
  17. //建立資料庫執行個體 db  這裡說明下:如果路徑中不存在"Test.db"的檔案,sqlite會自動建立"Test.db"  
  18. FMDatabase *db= [FMDatabase databaseWithPath:dbPath] ;  
  19. if (![db open]) {  
  20. NSLog(@"Could not open db.");  
  21. return ;  
  22. }    

接下來,我們可以通過這個資料庫物件進行操作了。操作主要是update和queries。

首先是建立表。

 
  1. //建立一個名為User的表,有兩個欄位分別為string類型的Name,integer類型的 Age     
  2. [db executeUpdate:@"CREATE TABLE User (Name text,Age integer)"];    
  3. //建立一個名為User的表,有兩個欄位分別為string類型的Name,integer類型的 Age  
  4. [db executeUpdate:@"CREATE TABLE User (Name text,Age integer)"];  

這樣我們就有了一張表了。接下我們對錶進行操作。插入資料!注意插入的資料使用了萬用字元,這跟iphone直接使用sqlite借口的綁定變數是一樣的,後面的萬用字元匹配的資料。

 
  1. //插入資料使用OC中的類型 text對應為NSString integer對應為NSNumber的整形     
  2. [db executeUpdate:@"INSERT INTO User (Name,Age) VALUES (?,?)",@"老婆",[NSNumber numberWithInt:20]]    
  3. //插入資料使用OC中的類型 text對應為NSString integer對應為NSNumber的整形  
  4. [db executeUpdate:@"INSERT INTO User (Name,Age) VALUES (?,?)",@"老婆",[NSNumber numberWithInt:20]]  

接下來是更新資料。

 
  1. //更新資料 將“老婆”更改為“寶貝”     
  2. [db executeUpdate:@"UPDATE User SET Name = ? WHERE Name = ? ",@"老婆",@"寶貝"];    
  3. //更新資料 將“老婆”更改為“寶貝”  
  4. [db executeUpdate:@"UPDATE User SET Name = ? WHERE Name = ? ",@"老婆",@"寶貝"];  

再接下來,就是刪除資料啦。

 
  1. //刪除資料     
  2. [db executeUpdate:@"DELETE FROM User WHERE Name = ?",@"老婆"];    
  3. //刪除資料  
  4. [db executeUpdate:@"DELETE FROM User WHERE Name = ?",@"老婆"];  

update的基本操作就這幾個,接下來是queries!

 
  1. //返回資料庫中第一條滿足條件的結果     
  2. NSString *aa=[db stringForQuery:@"SELECT Name FROM User WHERE Age = ?",@"20"];    
  3. //返回資料庫中第一條滿足條件的結果  
  4. NSString *aa=[db stringForQuery:@"SELECT Name FROM User WHERE Age = ?",@"20"];  

這樣我們就查詢返回了一條資料,那當我們想要查詢放返回多條資料怎麼辦呢?不用愁,之前我就提到了FMDB中的另外一個主要的類,FMResultSet,這是一個結果集!返回多條資料時FMDB會將資料放在這個結果集中,然後我們在對這個結果集進行查詢操作!很簡單。

 
  1. FMResultSet *rs=[db executeQuery:@"SELECT * FROM User"];    
  2. rs=[db executeQuery:@"SELECT * FROM User WHERE Age = ?",@"20"];    
  3. while ([rs next]){    
  4. NSLog(@"%@ %@",[rs stringForColumn:@"Name"],[rs stringForColumn:@"Age"]);    
  5. }    
  6. FMResultSet *rs=[db executeQuery:@"SELECT * FROM User"];  
  7. rs=[db executeQuery:@"SELECT * FROM User WHERE Age = ?",@"20"];  
  8. while ([rs next]){  
  9. NSLog(@"%@ %@",[rs stringForColumn:@"Name"],[rs stringForColumn:@"Age"]);  
  10. }  

更多的 FMResultSet方法有:

 
  1. intForColumn:   
  2. longForColumn:   
  3. longLongIntForColumn:   
  4. boolForColumn:   
  5. doubleForColumn:   
  6. stringForColumn:   
  7. dateForColumn:   
  8. dataForColumn:   
  9. dataNoCopyForColumn:   
  10. UTF8StringForColumnIndex:   
  11. objectForColumn:  

具體查看一下類就行了! 好了,對於FMDB的使用就這樣,是不是很簡單呢,其實這個些封裝sqlite的架構都是萬變不離其宗的,只要你掌握了sql就行了!

小結:iPhone資料儲存中關於Sqlite 3使用第三方架構FMDB的內容介紹完了,希望本文對你有所協助!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.