開源類庫之二 (FMDataBase)

FMDataBase是iOS平臺中一個非常強大的數據庫類庫,其將sqlite面向過程的接口以面向對象的方法展現出來,提供了極高的可用性。

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

  1. #import <Foundation/Foundation.h>  
  2. #import "FMDatabase.h"  
  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  
發佈了36 篇原創文章 · 獲贊 0 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章