FMDataBase是iOS平臺中一個非常強大的數據庫類庫,其將sqlite面向過程的接口以面向對象的方法展現出來,提供了極高的可用性。
其使用很簡單,將sqlite 庫添加到項目中,然後將FMDataBase類庫文件添加到項目中,下面是對筆者對FMDataBase進行的一個二次封裝,處理的數據庫很簡單,只有一張表,兩個列,存儲的都是一些key-value對,讀者可以根據自身需要對此類進行修改。
- #import <Foundation/Foundation.h>
- #import "FMDatabase.h"
- @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