//LoadData.h
#import "Model.h"
#import "FMDatabase.h"
//单列类
+(instancetype)sharlLoadData;
//添加元素
-(void)AddsharlLoadData:(Model *)model;
//查询
-(NSMutableArray *)Marr;
//删除元素
-(void)deleteharlLoadData:(Model *)model;
//修改元素
-(void)UPsharlLoadData:(Model *)model;
//LoadData.m
static LoadData *ld = nil;
static FMDatabase *fate;
//单列类
+(instancetype)sharlLoadData{
//静态
static dispatch_once_t oneet;
//初始化
dispatch_once(&oneet, ^{
ld = [[LoadData alloc]init];
//定义初始化
[ld initA];
});
//返回值
return ld;
}
//初始化
+(instancetype)allocWithZone:(struct _NSZone *)zone{
if (!ld) {
//初始化
ld = [super allocWithZone:zone];
}
return ld;
}
//浅复制
-(id)copy{
return self;
}
//深复制
-(id)mutableCopy{
return self;
}
//初始化数据库
-(void)initA{
//创建沙盒
NSString *Ste = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
//定义文件名
NSString *path = [Ste stringByAppendingPathComponent:@"HousingInfo.sqlite"];
//初始化
fate = [[FMDatabase alloc]initWithPath:path];
//判断
if ([fate open])
{
//初始化
[fate executeUpdate:@"create table class (ID integer primary key, fw text, zj text , fh text , xs text , bz text)"];
[fate close];
NSLog(@"成功");
}
else
{
NSLog(@"失败");
}
}
//添加元素
-(void)AddsharlLoadData:(Model *)model
{
//开始
[fate open];
//初始化
NSString *str = [NSString stringWithFormat:@"insert into class values (null , '%@','%@','%@','%@','%@')",model.fw,model.zj,model.fh,model.xs,model.bz];
//BOOL值接受
BOOL ii = [fate executeUpdate:str];
//判断
if (ii)
{
NSLog(@"成功");
}
else
{
NSLog(@"失败");
}
//关闭
[fate close];
}
//查询
-(NSMutableArray *)Marr{
//初始化
NSMutableArray *marr = [NSMutableArray new];
//开始
[fate open];
//初始化
FMResultSet *Set = [[FMResultSet alloc]init];
//使用set接受
Set = [fate executeQuery:@"select * from class"];
//判断
while ([Set next]) {
//初始化
Model *mm = [Model new];
//链接
mm.fw = [Set stringForColumn:@"fw"];
mm.zj = [Set stringForColumn:@"zj"];
mm.fh = [Set stringForColumn:@"fh"];
mm.bz = [Set stringForColumn:@"bz"];
mm.xs = [Set stringForColumn:@"xs"];
mm.ID = [Set intForColumn:@"ID"];
//添加到数组
[marr addObject:mm];
}
//关闭
[fate close];
//返回值
return marr;
}
//删除元素
-(void)deleteharlLoadData:(Model *)model{
//开始
[fate open];
//初始化
NSString *str = [NSString stringWithFormat:@"delete from class where ID = '%ld' ",model.ID];
//BOOL值接受
BOOL ii = [fate executeUpdate:str];
//判断
if (ii) {
NSLog(@"成功");
}else{
NSLog(@"失败");
}
//关闭
[fate close];
}
//修改元素
-(void)UPsharlLoadData:(Model *)model{
//开始
[fate open];
//初始化
NSString *str = [NSString stringWithFormat:@"update class set fw = '%@',zj = '%@',fh = '%@',xs = '%@',bz = '%@' where ID = '%ld'",model.fw,model.zj,model.fh,model.xs,model.bz,model.ID];
//BOOL值接受
BOOL ii = [fate executeUpdate:str];
//判断
if (ii) {
NSLog(@"成功");
}else{
NSLog(@"失败");
}
//关闭
[fate close];
}