FMDB的第三方库的使用

#import <Foundation/Foundation.h>
#import "AppModel.h"

@interface LimitDBManager : NSObject

+ (instancetype)sharedInstance;

//type :浏览,下载,收藏
//添加
- (void)addAppInfo:(AppModel*)model type:(NSString*)type;

//删除
- (void)deleteAppInfo:(AppModel*)model type:(NSString*)type;

//根据type读取AppInfo的列表
- (NSArray*)readAppInfoList:(NSString*)type;

//判断类型为type的app 是否存在表中
- (BOOL)isAppInfoExists:(AppModel*)model type:(NSString*)type;

@end

//=====================================================================//



#import "LimitDBManager.h"

#import "FMDatabase.h"

@interface LimitDBManager()
{
    FMDatabase *_db;   //数据库实例
}
@end

@implementation LimitDBManager


+ (instancetype)sharedInstance
{
    
    
    
    static LimitDBManager *s_dbManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        s_dbManager = [[LimitDBManager alloc]init];
    });
    return s_dbManager;
}

- (NSString*)dbPath
{
    return [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/limit.db"];
}

- (id)init{
    if (self = [super init]) {
        NSLog(@"dbPath = %@",[self dbPath]);
        _db  = [[FMDatabase alloc]initWithPath:[self dbPath]];
        if ([_db open]) {
            [self createTable];
        }
    }
    return self;
}

- (void)createTable
{
    NSString *sql = @"create table if not exists appInfo(serialId integer primary key autoincrement,appId text,appName text,appIconUrl text,appDesc text,type text)";
    if (![_db executeUpdate:sql]) {
        NSLog(@"创建表失败");
    }
}

//type :浏览,下载,收藏
//添加
- (void)addAppInfo:(AppModel*)model type:(NSString*)type
{
    NSString *sql = @"insert into appInfo(appId,appName,appIconUrl,appDesc,type) values(?,?,?,?,?)";
    if (![_db executeUpdate:sql,model.applicationId,model.name,model.iconUrl,model.description,type]) {
        NSLog(@"插入记录失败");
    }
}

//删除
- (void)deleteAppInfo:(AppModel*)model type:(NSString*)type
{
    NSString *sql = @"delete from appInfo where appId = ? AND type = ?";
    if (![_db executeUpdate:sql,model.applicationId,type]) {
        NSLog(@"删除数据失败");
    }
}

//根据type读取AppInfo的列表
- (NSArray*)readAppInfoList:(NSString*)type
{
    NSMutableArray *appArray = [NSMutableArray array];
    NSString *sql = @"select * from appInfo where type = ?";
    FMResultSet *resultSet = [_db executeQuery:sql,type];
    while (resultSet.next) {
        AppModel *appModel = [[AppModel alloc]init];
        appModel.applicationId = [resultSet stringForColumn:@"appId"];
        appModel.name          = [resultSet stringForColumn:@"appName"];
        appModel.iconUrl       = [resultSet stringForColumn:@"appIconUrl"];
        appModel.description   = [resultSet stringForColumn:@"appDesc"];
        [appArray addObject:appModel];
    }
    [resultSet close];
    return appArray;
}

//判断类型为type的app 是否存在表中
- (BOOL)isAppInfoExists:(AppModel*)model type:(NSString*)type{
    BOOL isExist = NO;
    NSString *sql = @"select * from appInfo where appId = ? and type = ?";
    FMResultSet *resultSet = [_db executeQuery:sql,model.applicationId,type];
    if (resultSet.next) {
        isExist = YES;
    }
    [resultSet close];
    return isExist;
}

@end

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值