首页 > 代码库 > 开源类库之二 (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=http://www.mamicode.com/? 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
开源类库之二 (FMDataBase)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。