使用 FMDB 的时候,存在插入,删除数据的时候出现崩溃??
iOS30 2015-08-07 10:49:04 //
// CollectTool.m
// DawdlerWeekend
//
// Created by lanouhn on 15/8/6.
// Copyright (c) 2015年 lanouhn. All rights reserved.
//
#import "CollectTool.h"
#import "FMDB.h"
#import "Root.h"
#import "FMDatabase.h"
@implementation CollectTool
static FMDatabase *_fmdb = nil;
+ (void)initialize{
// 1.文件路径
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]stringByAppendingPathComponent:@"Root.sqlite"];
NSLog(@"%@",filePath);
_fmdb = [FMDatabase databaseWithPath:filePath];
// 2. 打开数据库
if (![_fmdb open])
return;
[_fmdb executeUpdate:@"CREATE TABLE IF NOT EXISTS t_collect_info(id integer PRIMARY KEY,root blob NULL,leo_id text NOT NULL)"];
}
// 读取数据
+ (NSArray *)collectInfo:(int)page{
int size = 20;
int pos = (page - 1) * size;
FMResultSet *set = [_fmdb executeQueryWithFormat:@"SELECT * FROM t_collect_info ORDER BY id DESC LIMIT %d,%d",pos,size];
NSMutableArray *mRoot = [NSMutableArray array];
while (set.next) {
Root *root = [NSKeyedUnarchiver unarchiveObjectWithData:[set objectForColumnName:@"root"]];
[mRoot addObject:root];
}
return mRoot;
}
+ (void)addCollect:(Root *)root{
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:root];
BOOL isSuccess = [_fmdb executeUpdate:@"INSERT INTO t_collect_info(root,leo_id) VALUES (?,?)",data,root];
if (isSuccess) {
NSLog(@"添加成功!!");
}else{
NSLog(@"添加失败!!");
}
}
// 删除信息
+(void)removeCollect:(Root *)root{
BOOL isSuccess = [_fmdb executeUpdate:@"delete from t_collect_info where leo_id = ?",root];
if (isSuccess) {
NSLog(@"删除成功!!");
}else{
NSLog(@"删除失败!!");
}
}
// 信息是否存在
+ (BOOL)isCollected:(Root *)root{
FMResultSet *set = [_fmdb executeQueryWithFormat:@"SELECT count(*) AS info_count FROM t_collect_info WHERE leo_id = %@",root.leo_id];
[set next];
return [set intForColumn:@"info_count"] == 1;
}
@end