開源類庫之二 (FMDataBase),開源類庫fmdatabase
FMDataBase是iOS平台中一個非常強大的資料庫類庫,其將sqlite面向過程的介面以物件導向的方法展現出來,提供了極高的可用性。
其使用很簡單,將sqlite 庫添加到項目中,然後將FMDataBase類庫檔案添加到項目中,下面是對筆者對FMDataBase進行的一個二次封裝,處理的資料庫很簡單,只有一張表,兩個列,儲存的都是一些key-value對,讀者可以根據自身需要對此類進行修改。
[cpp] view plaincopy
- #import <Foundation/Foundation.h>
- #import "FMDatabase.h"
[cpp] view plaincopy
- @interface DBController : NSObject {
- }
-
- @property (nonatomic, assign) FMDatabase *dataBase;
-
- +(BOOL)databaseExit;
-
- -(BOOL)initDatabase;
-
- -(void)closeDatabase;
-
- -(BOOL)deleteTable;
-
- -(BOOL)InsertTable:(NSString *)key_type value:(NSString *)key_value;
-
- -(BOOL)UpdataTable:(NSString *) valueStr key:(NSString *)keyStr;
-
- -(NSMutableDictionary *)querryTable;
-
- +(BOOL) deleteDataBase;
-
- @end
-
- @synthesize dataBase = _dataBase;
-
- - (id)init{
-
- if(self = [super init]){
- _dataBase = [FMDatabase databaseWithPath: [DBController getPath]];
- if (![_dataBase open]) {
- NSLog(@"Create/Open dataBase %@ Failed!", [DBController getPath]);
- }
- }
- return self;
- }
-
- //資料庫是否存在
- +(BOOL)databaseExit
- {
- return [[NSFileManager defaultManager] fileExistsAtPath: [self getPath]];
- }
-
-
- //初始化資料庫
- -(BOOL)initDatabase{
- if ([DBController databaseExit]) {
- return [self createTable];
- }
- return NO;
- }
-
-
-
- //建立資料庫
- -(BOOL)createTable
- {
- return [self.dataBase executeUpdate: @"create table if not exists personTable(id integer primary key autoincrement, key text,value text);"];
- }
-
- //刪除資料表
- -(BOOL)deleteTable{
- if ([DBController databaseExit]) {
- return [self.dataBase executeUpdate: [NSString stringWithFormat:@"drop table %@;",_PERSONINFO]];
- }
- return NO;
- }
-
-
-
- //關閉資料庫
- - (void) closeDatabase
- {
- [self.dataBase close];
- }
-
- //插入資料
- -(BOOL)InsertTable:(NSString *)key value:(NSString *)value
- {
- if ([DBController databaseExit]) {
- BOOL result = NO;
- [self.dataBase beginTransaction];
- result = [self.dataBase executeUpdate:@"INSERT INTO personTable (key,value) VALUES (?,?)", key, value];
- [self.dataBase commit];
- return result;
- }
- return NO;
- }
-
- //更新資料
- -(BOOL)UpdataTable:(NSString *) valueStr key:(NSString *)keyStr
- {
- if ([DBController databaseExit]) {
- BOOL result = NO;
- [self.dataBase beginTransaction];
- result = [self.dataBase executeUpdate:@"UPDATE personTable SET value=? WHERE key=?", valueStr, keyStr];
- [self.dataBase commit];
- return result;
- }
- return NO;
- }
-
- //查詢整個表
- -(NSMutableDictionary *)querryTable
- {
- NSMutableDictionary* resultDic = [[NSMutableDictionary alloc] init];
-
- FMResultSet *rs = [self.dataBase executeQuery:@"select * from personTable"];
- while ([rs next]) {
- [resultDic setObject: [rs stringForColumn: @"value"] forKey: [rs stringForColumn: @"key"]];
- }
- return [resultDic autorelease];
- }
-
- +(BOOL) deleteDataBase
- {
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
-
- NSString *documentsDirectory = [paths objectAtIndex:0];
-
- NSString *path = [documentsDirectory stringByAppendingPathComponent:_DBNAME];//設定資料庫得路徑
-
- NSFileManager *fileManager = [NSFileManager defaultManager];
-
- BOOL find = [fileManager fileExistsAtPath:path];
-
- if (find) {
-
- [fileManager removeItemAtPath: path error: nil];
-
- }
- return find;
- }
-
-
- - (void)dealloc {
- [_dataBase close];
- _dataBase = nil;
- [super dealloc];
- }
-
- + (NSString*) getPath {
- // 開啟的資料庫
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
-
- NSString *documentsDirectory = [paths objectAtIndex:0];
-
- return [documentsDirectory stringByAppendingPathComponent:_DBNAME];//設定資料庫得路徑
- }
-
-
- @end