sqlite數據庫1

//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];

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容