DBAction.m 7.46 KB
//
//  DBAction.m
//  SCIOLive
//
//  Created by wei on 14-9-29.
//  Copyright (c) 2014年 Miao Long. All rights reserved.
//

#import "DBAction.h"
#import "FMDatabase.h"
FMDatabase * db = nil;
static DBAction *instance = nil;
@implementation DBAction

+ (DBAction *)sharedDBAction{
    if (instance == nil){
        instance = [[super allocWithZone:nil]init];
    }
    return instance;
}

+(id)allocWithZone:(NSZone *)zone{
    return [DBAction sharedDBAction];
}

//可写可不写
- (id)init{
    self = [super init];
    if (self) {
        db = [[FMDatabase alloc] initWithPath:SQLITE_PATH];
    }
    return self;
}

-(id)copy{
    return self;
}

- (id)copyWithZone:(NSZone *)zone{
    return self;
}

-(void)createNoticeTable{
    if ([db open]) {
        NSString * sql = @"CREATE TABLE NOTICE (time varchar, title varchar, content varchar)";
        BOOL res = [db executeUpdate:sql];
        if (!res) {
            NSLog(@"建表失败");
        } else {
            NSLog(@"建表成功");
        }
        [db close];
    } else {
        NSLog(@"打开失败");
    }
}

- (BOOL)isInNoticeTable:(NoticeData *)notice{
    if ([db open]) {
        NSString * check = [NSString stringWithFormat:@"SELECT * FROM NOTICE where time = %@",notice.time];
        FMResultSet * re = [db executeQuery:check];
        if ([re next]) {
            return YES;
        }
        [db close];
    }
    return NO;
}

- (void)insertNoticeTable:(NoticeData*)notice{
    if ([db open]) {
        NSString * check = [NSString stringWithFormat:@"SELECT * FROM NOTICE where time = %@",notice.time];
        FMResultSet * re = [db executeQuery:check];
        if ([re next]) {
            NSLog(@"已存在,进行更新操作");
            NSString * sql = [NSString stringWithFormat:@"UPDATE NOTICE SET title = '%@', content = '%@' WHERE time = %@'",notice.time,notice.title,notice.content];
            BOOL res = [db executeUpdate:sql];
            if (!res) {
                NSLog(@"error to insert data");
            } else {
                NSLog(@"succ to insert data");
            }
        }else{
            NSString * sql = [NSString stringWithFormat:@"INSERT INTO NOTICE (time, title, content) values('%@', '%@', '%@')",notice.time,notice.title,notice.content];
            BOOL res = [db executeUpdate:sql];
            if (!res) {
                NSLog(@"error to insert data");
            } else {
                NSLog(@"succ to insert data");
            }
        }
        [db close];
    }
}

- (void)deleteNotice:(NoticeData*)notice{
    if ([db open]) {
        NSString * sql = [NSString stringWithFormat:@"DELETE FROM NOTICE where time = %@",notice.time];
        BOOL res = [db executeUpdate:sql];
        if (!res) {
            NSLog(@"error to delete db data");
        } else {
            NSLog(@"succ to deleta db data");
        }
        [db close];
    }
}

- (NSMutableArray *)queryNoticeTable{
    NSMutableArray *list = [[NSMutableArray alloc] init];
    if ([db open]) {
        NSString * sql = @"SELECT * FROM NOTICE";
        FMResultSet * rs = [db executeQuery:sql];
        while ([rs next]) {
            NoticeData * notice = [[NoticeData alloc] init];
            notice.time  = [rs stringForColumn:@"time"];
            notice.title  = [rs stringForColumn:@"title"];
            notice.content  = [rs stringForColumn:@"content"];

            [list addObject:notice];
        }
        [db close];
    }
    return list;
}

- (void)createTable{
    if ([db open]) {
        NSString * sql = @"CREATE TABLE DATATABLE (cmsColumnName varchar, cmsColumnId varchar, cmsContentId int, createDate varchar, desc varchar, gxbArticleId int, gxbVideoId int, img varchar, pageUrl varchar, picType varchar, publishDate varchar, subTitle varchar, title varchar, type varchar, uploadVideoUrl varchar, videoUrl varchar, vipFlag int)";
        BOOL res = [db executeUpdate:sql];
        if (!res) {
            NSLog(@"建表失败");
        } else {
            NSLog(@"建表成功");
        }
        [db close];
    } else {
        NSLog(@"打开失败");
    }
}

- (BOOL)isInTable:(TableData*)notice{
    if ([db open]) {
        NSString * check = [NSString stringWithFormat:@"SELECT * FROM DATATABLE where cmsContentId = %d",notice.cmsContentId];
        FMResultSet * re = [db executeQuery:check];
        while ([re next])  {
            return YES;
        }
        [db close];
    }
    return NO;
}

- (BOOL)insertTable:(TableData*)notice{
    if ([db open]) {
        NSString * check = [NSString stringWithFormat:@"SELECT * FROM DATATABLE where cmsContentId = %d",notice.cmsContentId];
        FMResultSet * re = [db executeQuery:check];
        if (![re next]) {
            NSString *desc = [notice.desc stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
            NSString * sql = [NSString stringWithFormat:@"INSERT INTO DATATABLE (cmsColumnName, cmsColumnId, cmsContentId, createDate, desc, gxbArticleId, gxbVideoId, img, pageUrl, picType, publishDate, subTitle, title, type, uploadVideoUrl, videoUrl, vipFlag) values('%@', '%@', '%d', '%@', '%@', '%d', '%d', '%@', '%@', '%@', '%@', '%@', '%@','%@','%@', '%@', '%d')", notice.cmsColumnName, notice.cmsColumnId, notice.cmsContentId, notice.createDate, desc, notice.gxbArticleId,  notice.gxbVideoId, notice.img, notice.pageUrl, notice.picType, notice.publishDate, notice.subTitle, notice.title, notice.type, notice.uploadVideoUrl, notice.videoUrl, notice.vipFlag];
            BOOL res = [db executeUpdate:sql];
            if (!res) {
                return NO;
            } else {
                return YES;
            }
        }
        [db close];
    }
    return NO;
}

- (BOOL)deleteTable:(TableData*)notice{
    if ([db open]) {
        NSString * sql = [NSString stringWithFormat:@"DELETE FROM DATATABLE where cmsContentId = %d",notice.cmsContentId];
        BOOL res = [db executeUpdate:sql];
        if (!res) {
            return NO;
        } else {
            return YES;
        }
        [db close];
    }
    return NO;
}

- (NSMutableArray *)queryTable{
    NSMutableArray *list = [[NSMutableArray alloc] init];
    if ([db open]) {
        NSString * sql = @"SELECT * FROM DATATABLE";
        FMResultSet * rs = [db executeQuery:sql];
        while ([rs next]) {
            TableData * notice = [[TableData alloc] init];
            notice.cmsColumnName = [rs stringForColumn:@"cmsColumnName"];
            notice.cmsColumnId   = [rs stringForColumn:@"cmsColumnId"];
            notice.cmsContentId  = [rs    intForColumn:@"cmsContentId"];
            notice.createDate    = [rs stringForColumn:@"createDate"];
            notice.desc          = [rs stringForColumn:@"desc"];
            notice.gxbArticleId  = [rs    intForColumn:@"gxbArticleId"];
            notice.gxbVideoId    = [rs    intForColumn:@"gxbVideoId"];
            notice.img           = [rs stringForColumn:@"img"];
            notice.pageUrl       = [rs stringForColumn:@"pageUrl"];
            notice.picType       = [rs stringForColumn:@"picType"];
            notice.publishDate   = [rs stringForColumn:@"publishDate"];
            notice.subTitle      = [rs stringForColumn:@"subTitle"];
            notice.title         = [rs stringForColumn:@"title"];
            notice.type          = [rs stringForColumn:@"type"];
            notice.uploadVideoUrl= [rs stringForColumn:@"uploadVideoUrl"];
            notice.videoUrl      = [rs stringForColumn:@"videoUrl"];
            notice.vipFlag       = [rs    intForColumn:@"vipFlag"];
            [list addObject:notice];
        }
        [db close];
    }
    return list;
}
@end