简单的本地数据存储FMDB

  • 利用fmdb做了一个简单的本地数据存储,存储对象是字典,首先通过第三方cocospod集成 fmdb
  • pod 命令行

pod 'FMDB'复制代码
  • 创建一个新的类继承NSObject,代码如下:

.h

#import <Foundation/Foundation.h> 
@interface YMStatusCacheTool : NSObject
/** 存 */
+ (void)ym_saveWithStatus:(NSDictionary *)dataSoucre withParam:(NSString *)house_id;
//取数据
+ (NSDictionary *)ym_statuseWithParam:(NSString *)house_id;
//删
+ (void)ym_deleteWithParam:(NSString *)house_id;
@end
复制代码


.m

#import "YMStatusCacheTool.h"
#import <FMDB.h>
@implementation YMStatusCacheTool 
static FMDatabase * _db;
 
+ (void)initialize
{
    NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
    
    //拼接文件名
    NSString *filePath = [cachePath stringByAppendingString:@"status.sqlite"];
    //创建了一个数据库实例
    _db = [FMDatabase databaseWithPath:filePath];
    
    //打开数据库
    if ([_db open]) {
        NSLog(@"打开成功");
    } else {
        NSLog(@"打开失败");
    }
    
    //创建表格
    BOOL flag = [_db executeUpdate:@"create table if not exists t_status (id integer primary key autoincrement,house_id integer,dict,blob);"];
    
    if (flag) {
        NSLog(@"success to creating db table");
    } else {
        NSLog(@"error when creating db table");
    }
}复制代码
  • 插入数据

/** 插入数据 */
+ (void)ym_saveWithStatus:(NSDictionary *)dataSoucre withParam:(NSString *)house_id
{
    //打开数据库
    if ([_db open]) {
        //转为二进制数据
        NSData *data = [NSKeyedArchiver archivedDataWithRootObject:dataSoucre];
        BOOL flag = [_db executeUpdate:@"insert into t_status (house_id,dict) values(?,?)",house_id,data];
        if (flag) {
            NSLog(@"插入成功");
        } else {
            NSLog(@"插入失败");
        }
        [_db close];
    } else {
        NSLog(@"打开失败");
    }
    
}复制代码
  • 读取数据

//取数据
+ (NSDictionary *)ym_statuseWithParam:(NSString *)house_id
{
    //打开数据库
    NSDictionary *dict = [NSDictionary new];
    if ([_db open]) {
        //查询语句
        NSString *sql = [NSString stringWithFormat:@"select * from t_status where house_id = '%@';",house_id];
        FMResultSet *set = [_db executeQuery:sql];
        
        while ([set next]) {
            NSData *data = [set dataForColumn:@"dict"];
            dict = [NSKeyedUnarchiver unarchiveObjectWithData:data];
        }
        [_db close];
    } else {
        NSLog(@"打开失败");
    }
    
    return dict;
}
 
复制代码
  • 删除数据

//删
+ (void)ym_deleteWithParam:(NSString *)house_id
{
    //打开数据库
    if ([_db open]) {
        //删除语句
        NSString *sql = [NSString stringWithFormat:@"delete from t_status where house_id = '%@';",house_id];
        BOOL res = [_db executeUpdate:sql];
        if (!res) {
            NSLog(@"删除失败");
        } else {
            NSLog(@"删除成功");
        }
        [_db close];
    } else {
        NSLog(@"打开失败");
    }
}复制代码

这个demo没有很好的封装,也没有用上异步线程,缺陷还是蛮大的,当然对于新手也挺合适。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值