fmdb数据库

 

+(DataBaseHandle*)sharedInstanc

{

    static id handle;

    static dispatch_once_t once;

    dispatch_once(&once,^(){

        handle = [[DataBaseHandle alloc]init];

    });

    return handle;

}

 

//创建数据库路径

+(NSString*)databaseFilePath

{

    NSString * docPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;

    NSString * dbPath  = [docPath stringByAppendingString:@"dzm.sqlite"];

    

    NSLog(@"path ==== %@",dbPath);

    

    return dbPath;

}

static FMDatabase * _db = nil;

//创建数据库

+(void)creatDataBase

{

    _db = [FMDatabase databaseWithPath:[self databaseFilePath]];

}

 

//创建表

+(void)creatTable

{

    if (!_db) {

        [self creatDataBase];

    }

    //打开数据库,并判断数据库是否打开成功

    if (![_db open]) {

        NSLog(@"数据库打开失败");

        return;

    }

    //为数据库设置缓存,提高查询效率

    [_db setShouldCacheStatements:YES];

    

    //判断数据库中是否已经存在这个表,如果不存在则创建该表

    if(![_db tableExists:@"dzm"])

    {

        BOOL result = [_db executeUpdate:@"CREATE TABLE dzm(user_id TEXT PRIMARY KEY, data BLOB) "];

        if (result) {

            NSLog(@"创建成功");

        }

    }

    

//    [_db close];

}

//插入数据

+(void)insertDBWWithArra:(id)dataArray byID:(NSString*)model_id

{

    if (!_db) {

        [self creatDataBase];

    }

    if (![_db open]) {

        return;

    }

    [_db setShouldCacheStatements:YES];

    

    if (![_db tableExists:@"dzm"]) {

        [self creatTable];

    }

    //判断将要插入的数据是否存在

    //    NSString * model_id = [[modelDic allKeys] lastObject];

    NSMutableData * data = [NSMutableData data];

    NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];

    [archiver encodeObject:dataArray forKey:model_id];

    [archiver finishEncoding];

    FMResultSet * set = [_db executeQuery:@"select data from dzm where user_id = ?",model_id];

    if ([set next]) {

        BOOL result = [_db executeUpdate:@"update dzm set data = ? where user_id = ?",data,model_id];

        if (result) {

            NSLog(@"刷新成功");

        }

    }else{

        BOOL result = [_db executeUpdate:@"INSERT INTO dzm (user_id,data) VALUES (?,?)",model_id,data];

        NSLog(@"result === %d",result);

    }

    [_db close];

}

 

//通过ID获取数据

+(id)getDataArrayWithTitleid:(NSString*)titleID

{

    if (!_db) {

        [self creatDataBase];

    }

    if (![_db open]) {

        return nil;

    }

    if (![_db tableExists:@"dzm"]) {

        [_db close];

        return nil;

    }

    id array = nil;

    FMResultSet * set = [_db executeQuery:@"select data from dzm where user_id = ?",titleID];

    if ([set next]) {

        NSData * data = [set dataForColumn:@"data"];

        NSKeyedUnarchiver * unArchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];

        array = [unArchiver decodeObjectForKey:titleID];

        [unArchiver finishDecoding];

    }

    [_db close];

    return array;

}

 

//插入数据

+(void)insertDBWWithDictionary:(NSDictionary*)dataDic byID:(NSString*)model_id

{

    if (!_db) {

        [self creatDataBase];

    }

    if (![_db open]) {

        return;

    }

    

    [_db setShouldCacheStatements:YES];

    

    if (![_db tableExists:@"dzm"]) {

        [self creatTable];

    }

    

    //判断将要插入的数据是否存在

    //    NSString * model_id = [[modelDic allKeys] lastObject];

    NSMutableData * data = [NSMutableData data];

    NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];

    [archiver encodeObject:dataDic forKey:model_id];

    [archiver finishEncoding];

    

    FMResultSet * set = [_db executeQuery:@"select data from dzm where user_id = ?",model_id];

    if ([set next]) {

        BOOL result = [_db executeUpdate:@"update dzm set data = ? where user_id = ?",data,model_id];

        if (result) {

            NSLog(@"刷新成功");

        }

    }else{

        BOOL result = [_db executeUpdate:@"INSERT INTO dzm (user_id,data) VALUES (?,?)",model_id,data];

        NSLog(@"result === %d 插入成功!",result);

    }

    

    [_db close];

}

 

//通过ID获取数据

+(NSDictionary*)getDataDictionaryWithTitleid:(NSString*)titleID

{

    if (!_db) {

        [self creatDataBase];

    }

    if (![_db open]) {

        return nil;

    }

    if (![_db tableExists:@"dzm"]) {

        [_db close];

        return nil;

    }

    

    NSDictionary * dic = nil;

    FMResultSet * set = [_db executeQuery:@"select data from dzm where user_id = ?",titleID];

    if ([set next]) {

        NSData * data = [set dataForColumn:@"data"];

        NSKeyedUnarchiver * unArchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];

        dic = [unArchiver decodeObjectForKey:titleID];

        [unArchiver finishDecoding];

    }

    [_db close];

    return dic;

}

 

 

//更新数据库

+(void)updateDataBaseWithDictionary:(NSDictionary*)modelDic

{

    if (!_db) {

        [self creatDataBase];

    }

    if (![_db open]) {

        return;

    }

    

    [_db setShouldCacheStatements:YES];

    

    if (![_db tableExists:@"dzm"]) {

        [self creatTable];

    }

    

    //判断将要插入的数据是否存在

    NSString * model_id = [[modelDic allKeys] lastObject];

    NSMutableData * data = [NSMutableData data];

    NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];

    [archiver encodeObject:modelDic forKey:model_id];

    [archiver finishEncoding];

    

    FMResultSet * set = [_db executeQuery:@"select  * from dzm where user_id = ?",model_id];

    if ([set next]) {

        [_db executeUpdate:@"update dzm set data = ? where user_id = ?",data,model_id];

    }

    [_db close];

}

 

//通过ID删除数据

+(void)deleteDataByTitleID:(NSString*)titleID

{

    

    if (![_db open]) {

        return;

    }

    

    [_db setShouldCacheStatements:YES];

    

    if (![_db tableExists:@"dzm"]) {

        return;

    }

    

    [_db executeUpdate:@"delete from dzm where user_id = ?",titleID];

    

    [_db close];

}

 

//删除所有数据

+(void)deleteAllData

{

    if (![_db open]) {

        return;

    }

    

    [_db setShouldCacheStatements:YES];

    

    if (![_db tableExists:@"dzm"]) {

        return;

    }

    

    [_db executeUpdate:@"delete from dzm"];

    

    [_db close];

}

 

转载于:https://www.cnblogs.com/flyios/p/6590337.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值