開源類庫之二 (FMDataBase),開源類庫fmdatabase

來源:互聯網
上載者:User

開源類庫之二 (FMDataBase),開源類庫fmdatabase

FMDataBase是iOS平台中一個非常強大的資料庫類庫,其將sqlite面向過程的介面以物件導向的方法展現出來,提供了極高的可用性。

其使用很簡單,將sqlite 庫添加到項目中,然後將FMDataBase類庫檔案添加到項目中,下面是對筆者對FMDataBase進行的一個二次封裝,處理的資料庫很簡單,只有一張表,兩個列,儲存的都是一些key-value對,讀者可以根據自身需要對此類進行修改。

[cpp] view plaincopy
  1. #import <Foundation/Foundation.h>  
  2. #import "FMDatabase.h"  
[cpp] view plaincopy
  1. @interface DBController : NSObject {  
  2. }  
  3.   
  4. @property (nonatomic, assign) FMDatabase *dataBase;  
  5.   
  6. +(BOOL)databaseExit;  
  7.   
  8. -(BOOL)initDatabase;  
  9.   
  10. -(void)closeDatabase;  
  11.   
  12. -(BOOL)deleteTable;  
  13.   
  14. -(BOOL)InsertTable:(NSString *)key_type value:(NSString *)key_value;  
  15.   
  16. -(BOOL)UpdataTable:(NSString *) valueStr key:(NSString *)keyStr;  
  17.   
  18. -(NSMutableDictionary *)querryTable;  
  19.   
  20. +(BOOL) deleteDataBase;  
  21.   
  22. @end  
  23.   
  24. @synthesize dataBase = _dataBase;  
  25.   
  26. - (id)init{  
  27.       
  28.     if(self = [super init]){  
  29.         _dataBase = [FMDatabase databaseWithPath: [DBController getPath]];  
  30.         if (![_dataBase open]) {  
  31.             NSLog(@"Create/Open dataBase %@ Failed!", [DBController getPath]);  
  32.         }   
  33.     }  
  34.     return self;  
  35. }  
  36.   
  37. //資料庫是否存在  
  38. +(BOOL)databaseExit  
  39. {  
  40.     return [[NSFileManager defaultManager] fileExistsAtPath: [self getPath]];  
  41. }  
  42.   
  43.   
  44. //初始化資料庫  
  45. -(BOOL)initDatabase{      
  46.     if ([DBController databaseExit]) {  
  47.         return [self createTable];  
  48.     }  
  49.     return NO;  
  50. }  
  51.   
  52.   
  53.   
  54. //建立資料庫  
  55. -(BOOL)createTable  
  56. {  
  57.     return [self.dataBase executeUpdate: @"create table if not exists personTable(id integer primary key autoincrement, key text,value text);"];  
  58. }  
  59.   
  60. //刪除資料表  
  61. -(BOOL)deleteTable{  
  62.     if ([DBController databaseExit]) {  
  63.         return [self.dataBase executeUpdate: [NSString stringWithFormat:@"drop table %@;",_PERSONINFO]];          
  64.     }  
  65.     return NO;  
  66. }  
  67.   
  68.   
  69.   
  70. //關閉資料庫  
  71. - (void) closeDatabase  
  72. {  
  73.     [self.dataBase close];  
  74. }  
  75.   
  76. //插入資料  
  77. -(BOOL)InsertTable:(NSString *)key value:(NSString *)value  
  78. {  
  79.     if ([DBController databaseExit]) {  
  80.         BOOL result = NO;  
  81.         [self.dataBase beginTransaction];  
  82.         result = [self.dataBase executeUpdate:@"INSERT INTO personTable (key,value) VALUES (?,?)", key, value];  
  83.         [self.dataBase commit];  
  84.         return result;  
  85.     }  
  86.     return NO;  
  87. }  
  88.   
  89. //更新資料  
  90. -(BOOL)UpdataTable:(NSString *) valueStr key:(NSString *)keyStr  
  91. {  
  92.     if ([DBController databaseExit]) {  
  93.         BOOL result = NO;  
  94.         [self.dataBase beginTransaction];  
  95.         result = [self.dataBase executeUpdate:@"UPDATE personTable SET value=? WHERE key=?", valueStr, keyStr];  
  96.         [self.dataBase commit];  
  97.         return result;  
  98.     }  
  99.     return NO;  
  100. }  
  101.   
  102. //查詢整個表  
  103. -(NSMutableDictionary *)querryTable  
  104. {  
  105.     NSMutableDictionary* resultDic = [[NSMutableDictionary alloc] init];  
  106.       
  107.     FMResultSet *rs = [self.dataBase executeQuery:@"select * from personTable"];  
  108.     while ([rs next]) {  
  109.         [resultDic setObject: [rs stringForColumn: @"value"] forKey: [rs stringForColumn: @"key"]];  
  110.     }  
  111.     return [resultDic autorelease];  
  112. }  
  113.   
  114. +(BOOL) deleteDataBase  
  115. {  
  116.     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  117.       
  118.     NSString *documentsDirectory = [paths objectAtIndex:0];  
  119.       
  120.     NSString *path = [documentsDirectory stringByAppendingPathComponent:_DBNAME];//設定資料庫得路徑  
  121.       
  122.     NSFileManager *fileManager = [NSFileManager defaultManager];  
  123.       
  124.     BOOL find = [fileManager fileExistsAtPath:path];  
  125.       
  126.     if (find) {  
  127.           
  128.         [fileManager removeItemAtPath: path error: nil];  
  129.           
  130.     }  
  131.     return find;  
  132. }  
  133.   
  134.   
  135. - (void)dealloc {  
  136.     [_dataBase close];  
  137.     _dataBase = nil;  
  138.     [super dealloc];  
  139. }  
  140.   
  141. + (NSString*) getPath {  
  142.     // 開啟的資料庫  
  143.     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  144.       
  145.     NSString *documentsDirectory = [paths objectAtIndex:0];  
  146.       
  147.     return [documentsDirectory stringByAppendingPathComponent:_DBNAME];//設定資料庫得路徑  
  148. }  
  149.   
  150.   
  151. @end  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.