// DataManger.h
@interface DataManger : NSObject
+(DataManger *)datamanger;
//名字一定要取的规范
-(void)zeng:(id)movie;
-(void)shan:(int)title;
-(id)chaxun;
@end
// DataManger.m
#import “Model.h”
#import “FMDB.h”
static DataManger * _default = nil;
@interface DataManger ()
@property(nonatomic,strong)FMDatabase*fMDB;
@end
+(DataManger *)datamanger{
if (_default == nil) {
_default =[[DataManger alloc] init];
}
return _default;
}
+(instancetype)allocWithZone:(struct _NSZone *)zone{
if (_default == nil) {
_default = [super allocWithZone:zone];
}
return _default;
}
-(FMDatabase *)fMDB{
if (!_fMDB) {
NSString *path=[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@“movies.sqlite”];
NSLog(@"%@",path);
_fMDB = [FMDatabase databaseWithPath:path];
[self inittable];
}
return _fMDB;
}
-(void)inittable{
[_fMDB open];
[_fMDB executeUpdate:@“CREATE TABLE movie (id INTEGER PRIMARY KEY AUTOINCREMENT,content TEXT,updatetime TEXT)”];
[_fMDB close];
}
//曾
-(void)zeng:(dataModel *)movie{
[self.fMDB open];
[self.fMDB executeUpdateWithFormat:@“insert into movie (content,updatetime) values (%@,%@)”,movie.content,movie.updatetime];
[self.fMDB close];
}
//删
-(void)shan:(int)title{
[self.fMDB open];
[self.fMDB executeUpdateWithFormat:@“DELETE FROM movie WHERE id = %d”,title];
[self.fMDB close];
}
//查
-(id)chamovie{
[self.fMDB open];
NSMutableArray *arr=[NSMutableArray new];
FMResultSet *result=[self.fMDB executeQuery:@“SELECT * FROM movie”];
while ([result next]) {
dataModel *movie=[dataModel new];
[arr addObject:movie];
//ID永远排第一
movie.ID= [result intForColumnIndex:0];
movie.content=[result stringForColumnIndex:1];
movie.updatetime=[result stringForColumnIndex:2];
}
[self.fMDB close];
return [arr copy];
}