FNDB数据库

来源:互联网 发布:ubuntu下载火狐浏览器 编辑:程序博客网 时间:2024/06/05 13:25

// 点h文件的操作

#import

// 引入FMDB

#import "FMDatabase.h"

@interface CollectDataHandle : NSObject


// 创建将要收藏的数据来进行收藏

{

    NSMutableData *_collectNewsData;

}

// 创建数据库

@property (nonatomic, strong)FMDatabase *db;


#pragma mark 创建单例对象

 

+ (CollectDataHandle *)shareNewsDataHandle;


#pragma mark 插入数据

- (void)insert:(DetailModel *)sender;


#pragma mark 删除数据

- (void)delete:(NSString *)title;


#pragma mark 取出数据,用数组来进行接收

- (NSArray *)getNewsAllData;


// 点m文件的操作

#import "CollectDataHandle.h"

static CollectDataHandle *newsDataHandle;

@implementation CollectDataHandle

// 创建单例的方法

+ (CollectDataHandle *)shareNewsDataHandle

 

{

    

    if (newsDataHandle == nil) {

        newsDataHandle = [[[self class] alloc] init];

        [newsDataHandle creatTable];

    }

    return newsDataHandle;

}


//  创建数据库

- (void)creatTable

{

    // 1.获得数据库文件的路径

    NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES) lastObject];

    NSString *fileName = [doc stringByAppendingPathComponent:@"newsTable.sqlite"];

    NSLog(@"%@", fileName);

    

    

    // 2.获得数据库

    FMDatabase *db = [FMDatabase databaseWithPath:fileName];

    

    // 3.打开数据库

    if ([db open]) {

        NSLog(@"数据库打开成功");

        BOOL result = [db executeUpdate:@"CREATE TABLE IF NOT EXISTS news (id integer PRIMARY KEY AUTOINCREMENT, title text NOT NULL, author text NOT NULL, date text NOT NULL, pic text NOT NULL, scont text NOT NULL, url text NOT NULL);"];

        if (result) {

            NSLog(@"创建成功");

        } else{

            NSLog(@"创建失败");

            [db close];

        }

    }

// 把创建好的数据库交付给之前的指针

    self.db = db;

    [db close];


}


// 插入数据(问题出在这里)

- (void)insert:(DetailModel *)sender

{

    //  存入数据库

    [_db open];

    NSLog(@"%@", sender.title);

   // 通过已经封装好的方法判断数据是否已经被收藏

    if (YES == [self query:sender.title]) {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"您已经收藏过了喔" message: @"去看看其他的吧^_^" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

        [alert show];

        [alert release];

    } else {

        [_db executeUpdate:@"INSERT INTO news (title, author, date, pic, scont, url) VALUES (?, ?, ?, ?, ?, ?);", sender.title, sender.author, sender.date, sender.pic, sender.scont, sender.url];

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"收藏成功" message: @"^_^" delegate:selfcancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

        [alert show];

        NSLog(@"插入成功");

    }

    

    [_db close];

    

}


// 查询

- (BOOL)query:(NSString *)title

{

    // 1.执行查询语句

    FMResultSet *resultSet = [self.db executeQuery:@"SELECT * FROM news WHERE title = ?", title];

    // 2.遍历结果

    while ([resultSet next]) {

        return YES;

    }

    return NO;

}



// 删除数据

- (void)delete:(NSString *)title

{

    // 删除所有数据

    [_db open];

    [_db executeUpdate:@"DELETE FROM news WHERE title = ?",title];

    [_db close];

}




- (NSArray *)getNewsAllData

{

    [_db open];

    NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];

    FMResultSet *r = [_db executeQuery:@"SELECT * FROM news"];

    while ([r next]) {

        NSString *title = [r stringForColumn:@"title"];

        NSString *author = [r stringForColumn:@"author"];

        NSString *date = [r stringForColumn:@"date"];

        NSString *scont = [r stringForColumn:@"scont"];

        NSString *pic = [r stringForColumn:@"pic"];

        NSString *url = [r stringForColumn:@"url"];

        

        DetailModel *detailM = [DetailModel new];

        detailM.title = title;

        detailM.author = author;

        detailM.date = date;

        detailM.scont = scont;

        detailM.pic = pic;

        detailM.url = url;

        [array addObject:detailM];

        [detailM release];

    }

    [_db close];

    

    NSMutableArray *arraySort = [NSMutableArray arrayWithCapacity:0];

    NSInteger num = [array count];

    for (int i = 0; i < num; i++) {

        [arraySort addObject:array[num - i - 1]];

    }

    return arraySort;


}


#pragma mark - 重写

#pragma mark 重写dealloc

- (void)dealloc

{

    [_collectNewsData release];

    

    [super dealloc];

}



 

@end


@end

0 0
原创粉丝点击