美文网首页
早期笔记——数据库工具类

早期笔记——数据库工具类

作者: 夏天的枫_ | 来源:发表于2020-07-21 11:48 被阅读0次

    总结:对于数据库才做应该独立到一个类,而且要去单例模式。

    #import "DBManager.h"
    #import "FMDatabase.h"
    #import "LimitFreeModel.h"
    
    static DBManager * m_DBManger;
    
    @interface DBManager ()
    
    @property (nonatomic,strong)FMDatabase * db;
    
    @end
    
    @implementation DBManager
    
    //单例
    +(DBManager *)sharedDBManager
    {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            m_DBManger = [[DBManager alloc] init];
        });//采取了更为准确的单例模式
        return m_DBManger;
    }
    - (id)init
    {
        if (self = [super init]) {
            NSString * dbPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/db.sqlite"];
            self.db =[FMDatabase databaseWithPath:dbPath];
            if (![self.db open]) {
                NSLog(@"打开数据库失败");
            }
            //创建表
            NSString * sql = @"create table if not exists t_favourite(id INTEGER PRIMARY KEY autoincrement,applicationID text UNIQUE,appname text)";
    
            [self.db executeUpdate:sql];
        }
        return self;
    }
    //增
    - (void)addFavorite:(LimitFreeModel *)model
    {
        NSString * sql = @"INSERT INTO t_favourite(applicationID,appname) values (?,?)";
        [self.db executeUpdate:sql,model.applicationId,model.name];
    }
    //删
    - (void)removefavorite:(LimitFreeModel *)model
    {
        NSString * sql = @"DELETE FROM t_favourite where applicationID = ?";
    
        [self.db executeUpdate:sql,model.applicationId];
    
    }
    //判断是否已经存储过
    - (BOOL)isExistFavorite:(LimitFreeModel *)model
    {
        NSString * sql = @"SELECT * FROM t_favourite WHERE applicationID = ?";
    
        FMResultSet * result = [self.db executeQuery:sql,model.applicationId];
        while ([result next]) {
            return YES;
        }
        return NO;
    }
    //查询
    - (NSArray *)getAllFavorites
    {
        NSString * sql = @"SELECT * FROM t_favourite";
    
        FMResultSet * result = [self.db executeQuery:sql];
        NSMutableArray * resultArray = [NSMutableArray new];
        while ([result next]) {
            LimitFreeModel * model = [[LimitFreeModel alloc] init];
            model.applicationId = [result stringForColumn:@"applicationID"];
            model.name = [result stringForColumn:@"appname"];
            [resultArray addObject:model];
        }
        return resultArray;
    }
    
    
    //7,更新数据(改)
    -(void)updateUserData:(NSString *)updateName replaceName:(NSString *)replaceName{
        NSString * sql = @"update User set name = ? where name = ?";
        BOOL isOK = [dataBase executeUpdate:sql,replaceName,updateName];
        if (isOK) {
            NSLog(@"更新成功");
        }else{
            NSLog(@"更新失败");
        }
    }
    

    二、调用单例解决问题

        if ([[DBManager sharedDBManager] isExistFavorite:self.model]) {
    
            [[DBManager sharedDBManager] removefavorite:self.model];
    
            [self.favoriteBtn setTitle:@"收藏" forState:UIControlStateNormal];
    
        }
        else{
            [[DBManager sharedDBManager]addFavorite:self.model];
            [self.favoriteBtn setTitle:@"取消收藏" forState:UIControlStateNormal];
        }

    相关文章

      网友评论

          本文标题:早期笔记——数据库工具类

          本文链接:https://www.haomeiwen.com/subject/pscikktx.html