缓存模型到数据库中

2018-01-02  本文已影响7人  huicuihui

缓存使用的数据库。使用第三方FMDB。
创建一个继承自NSObject类的文件去封装管理缓存的操作。
增删改查。
数据模型: NEWModel
1:保存数据到数据库中。

- (void) saveNews:(NEWModel *) news{
    
    NSMutableString * query = [NSMutableString stringWithFormat:@"INSERT INTO newsTB"];
    NSMutableString * keys = [NSMutableString stringWithFormat:@" ("];
    NSMutableString * values = [NSMutableString stringWithFormat:@" ( "];
    NSMutableArray * arguments = [NSMutableArray arrayWithCapacity:5];
    
//    [keys appendString:@"hasRead,"];
//    [values appendString:@"?,"];
//    [arguments addObject:@(0)];
    
    if (news.newsDetail) {
        [keys appendString:@"newsDetail,"];
        [values appendString:@"?,"];
        [arguments addObject:news.newsDetail];
    }
    if (news.articleID) {
        [keys appendString:@"articleID,"];
        [values appendString:@"?,"];
        [arguments addObject:news.articleID];
    }
    if (news.title) {
        [keys appendString:@"title,"];
        [values appendString:@"?,"];
        [arguments addObject:news.title];
    }
    if (news.createDate) {
        [keys appendString:@"createDate,"];
        [values appendString:@"?,"];
        [arguments addObject:news.createDate];
    }
    if (news.editDate) {
        [keys appendString:@"editDate,"];
        [values appendString:@"?,"];
        [arguments addObject:news.editDate];
    }
    if (news.topDate) {
        [keys appendString:@"topDate,"];
        [values appendString:@"?,"];
        [arguments addObject:news.topDate];
    }
    if (news.modifyedDate) {
        [keys appendString:@"modifyedDate,"];
        [values appendString:@"?,"];
        [arguments addObject:news.modifyedDate];
    }
    if (news.tag) {
        [keys appendString:@"tag,"];
        [values appendString:@"?,"];
        [arguments addObject:news.tag];
    }
    if (news.author) {
        [keys appendString:@"author,"];
        [values appendString:@"?,"];
        [arguments addObject:news.author];
    }
    if (news.commentTimes) {
        [keys appendString:@"commentTimes,"];
        [values appendString:@"?,"];
        [arguments addObject:news.commentTimes];
    }
    if (news.createBy) {
        [keys appendString:@"createBy,"];
        [values appendString:@"?,"];
        [arguments addObject:news.createBy];
    }
    
    if (news.dispType) {
        [keys appendString:@"dispType,"];
        [values appendString:@"?,"];
        [arguments addObject:news.dispType];
    }
    if (news.modifiedBy) {
        [keys appendString:@"modifiedBy,"];
        [values appendString:@"?,"];
        [arguments addObject:news.modifiedBy];
    }
    if (news.picOne) {
        [keys appendString:@"picOne,"];
        [values appendString:@"?,"];
        [arguments addObject:news.picOne];
    }
    if (news.picTwo) {
        [keys appendString:@"picTwo,"];
        [values appendString:@"?,"];
        [arguments addObject:news.picTwo];
    }
    if (news.picThree) {
        [keys appendString:@"picThree,"];
        [values appendString:@"?,"];
        [arguments addObject:news.picThree];
    }
    if (news.realLikeTimes) {
        [keys appendString:@"realLikeTimes,"];
        [values appendString:@"?,"];
        [arguments addObject:news.realLikeTimes];
    }
    if (news.realReadTimes) {
        [keys appendString:@"realReadTimes,"];
        [values appendString:@"?,"];
        [arguments addObject:news.realReadTimes];
    }
    if (news.startLikeTimes) {
        [keys appendString:@"startLikeTimes,"];
        [values appendString:@"?,"];
        [arguments addObject:news.startLikeTimes];
    }
    if (news.startReadTimes) {
        [keys appendString:@"startReadTimes,"];
        [values appendString:@"?,"];
        [arguments addObject:news.startReadTimes];
    }
    if (news.source) {
        [keys appendString:@"source,"];
        [values appendString:@"?,"];
        [arguments addObject:news.source];
    }
    if (news.topFlag) {
        [keys appendString:@"topFlag,"];
        [values appendString:@"?,"];
        [arguments addObject:news.topFlag];
    }
    if (news.showTime) {
        [keys appendString:@"showTime,"];
        [values appendString:@"?,"];
        [arguments addObject:news.showTime];
    }
    if (news.isTopNews) {
        [keys appendString:@"isTopNews,"];
        [values appendString:@"?,"];
        [arguments addObject:news.isTopNews];
    }
    if (news.isAccountingNews) {
        [keys appendString:@"isAccountingNews,"];
        [values appendString:@"?,"];
        [arguments addObject:news.isAccountingNews];
    }
    if (news.isTaxmanageNews) {
        [keys appendString:@"isTaxmanageNews,"];
        [values appendString:@"?,"];
        [arguments addObject:news.isTaxmanageNews];
    }
    
    [keys appendString:@")"];
    [values appendString:@")"];
    [query appendFormat:@" %@ VALUES%@;",
     [keys stringByReplacingOccurrencesOfString:@",)" withString:@")"],
     [values stringByReplacingOccurrencesOfString:@",)" withString:@")"]];
    NSLog(@"%@",query);
    BOOL isSucess =     [_db executeUpdate:query withArgumentsInArray:arguments];
    if (!isSucess) {
        NSLog(@"插入财税头条数据失败");
    }
    
}

2:修改是否是已经读过的属性。其他的都不变,只修改这一个属性的话,可以只更新该条数据的这一个字段:

- (void) mergeWithArticleID:(NEWModel *) news{
    if (!news.articleID) {
        return;
    }
    [_db open];
    NSString * query = @"UPDATE newsTB SET";
    NSMutableString * temp = [NSMutableString stringWithCapacity:20];
    
    [temp appendFormat:@" hasRead = '%@'",@(1)];

    query = [query stringByAppendingFormat:@"%@ WHERE articleID = '%@'",[temp stringByReplacingOccurrencesOfString:@",)" withString:@""],news.articleID];
    NSLog(@"%@",query);
    
    if ([_db executeUpdate:query]) {
        NSLog(@"更新成功");
    }
    [_db close];
}

注:
一定要测试一下executeUpdate是否返回YES,不是的话就要去查看是否是sql语句最后多一个,的问题。 [temp appendFormat:@" hasRead = '%@'",@(1)];

网络请求成功回来数据之后,需要把本地数据库缓存的数据也都更新了:

- (void) mergeWithNews:(NEWModel *) news{
    if (!news.articleID) {
        return;
    }
    NSString * query = @"UPDATE newsTB SET";
    NSMutableString * temp = [NSMutableString stringWithCapacity:20];
    // xxx = xxx;
    if (news.newsDetail) {
        [temp appendFormat:@" newsDetail = '%@',",news.newsDetail];
    }
    
    if (news.articleID) {
        [temp appendFormat:@" articleID = '%@',",news.articleID];
    }
    if (news.title) {
        [temp appendFormat:@" title = '%@',",news.title];
    }
    if (news.createDate) {
        [temp appendFormat:@" createDate = '%@',",news.createDate];
    }
    if (news.editDate) {
        [temp appendFormat:@" editDate = '%@',",news.editDate];
        
    }
    if (news.topDate) {
        [temp appendFormat:@" topDate = '%@',",news.topDate];
    }
    if (news.modifyedDate) {
        [temp appendFormat:@" modifyedDate = '%@',",news.modifyedDate];
    }
    if (news.tag) {
        [temp appendFormat:@" tag = '%@',",news.tag];
    }
    if (news.author) {
        [temp appendFormat:@" author = '%@',",news.author];
    }
    if (news.commentTimes) {
        [temp appendFormat:@" commentTimes = '%@',",news.commentTimes];
    }
    if (news.createBy) {
        [temp appendFormat:@" createBy = '%@',",news.createBy];
    }
    
    if (news.dispType) {
        [temp appendFormat:@" dispType = '%@',",news.dispType];
    }
    if (news.modifiedBy) {
        [temp appendFormat:@" modifiedBy = '%@',",news.modifiedBy];
    }
    if (news.picOne) {
        [temp appendFormat:@" picOne = '%@',",news.picOne];
    }
    if (news.picTwo) {
        [temp appendFormat:@" picTwo = '%@',",news.picTwo];
    }
    if (news.picThree) {
        [temp appendFormat:@" picThree = '%@',",news.picThree];
    }
    if (news.realLikeTimes) {
        [temp appendFormat:@" realLikeTimes = '%@',",news.realLikeTimes];
    }
    if (news.realReadTimes) {
        [temp appendFormat:@" realReadTimes = '%@',",news.realReadTimes];
    }
    if (news.startLikeTimes) {
        [temp appendFormat:@" startLikeTimes = '%@',",news.startLikeTimes];
        
    }
    if (news.startReadTimes) {
        [temp appendFormat:@" startReadTimes = '%@',",news.startReadTimes];
    }
    if (news.source) {
        [temp appendFormat:@" source = '%@',",news.source];
    }
    if (news.topFlag) {
        [temp appendFormat:@" topFlag = '%@',",news.topFlag];
    }
    if (news.showTime) {
        [temp appendFormat:@" showTime = '%@',",news.showTime];
    }
    if (news.isTopNews) {
        [temp appendFormat:@" isTopNews = '%@',",news.isTopNews];
    }
    if (news.isAccountingNews) {
        [temp appendFormat:@" isAccountingNews = '%@',",news.isAccountingNews];
    }
    if (news.isTaxmanageNews) {
        [temp appendFormat:@" isTaxmanageNews = '%@',",news.isTaxmanageNews];
    }
    
    NSString *sqlStr = [temp substringToIndex:temp.length - 1];
    query = [query stringByAppendingFormat:@"%@ WHERE articleID = '%@'",[sqlStr stringByReplacingOccurrencesOfString:@",)" withString:@""],news.articleID];
    
    if (![_db executeUpdate:query]) {
        NSLog(@"更新失败");
    }
}

删除某一条数据

- (void) deleteNewsWithArticleID:(NSString *) articleID{
    NSString * query = [NSString stringWithFormat:@"DELETE FROM newsTB WHERE articleID = '%@'",articleID];
    [_db executeUpdate:query];
}
上一篇 下一篇

猜你喜欢

热点阅读