首页 > 代码库 > FMDB----SQL----数据库

FMDB----SQL----数据库

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #822e0e } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3d1d81 } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; min-height: 21.0px } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #539aa4 } p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1d9421 } p.p7 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c91b13 } p.p8 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px "PingFang SC"; color: #1d9421 } p.p9 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c32275 } p.p10 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #6122ae } span.s1 { } span.s2 { font: 18.0px "PingFang SC" } span.s3 { color: #c32275 } span.s4 { color: #1d9421 } span.s5 { font: 18.0px "PingFang SC"; color: #1d9421 } span.s6 { color: #000000 } span.s7 { color: #6122ae } span.s8 { color: #c91b13 } span.s9 { color: #539aa4 } span.s10 { color: #3c828c } span.s11 { color: #3d1d81 } span.s12 { color: #294c50 } span.s13 { color: #703daa } span.s14 { color: #0435ff } span.s15 { font: 18.0px Menlo; color: #000000 } span.s16 { font: 18.0px Menlo }</style>

#pragma mark -- 数库

- (void)createDatabase{

    //路径

    NSString *path = [NSString stringWithFormat:@"%@/Documents/maxueshan.db",NSHomeDirectory()];

    //创建

    _database = [[FMDatabase alloc]initWithPath:path];

    

    //打开

    [_database open];

    

    NSLog(@"%@",path);

    

}

//重写析构方法   ,  不能写 [super dealloc]

- (void)dealloc{

    

    //关闭数据库

    [_database close];

    

}

 

#pragma mark -- 创建表格

- (void)createTable{

    

    NSString *sql = @"create table if not exists Student(stuName text,stuAge text, stuSex text)";

    BOOL isSuccess = [_database executeUpdate:sql];

    if (isSuccess) {

        NSLog(@"table表格创建成功");

    }else {

        NSLog(@"table表格创建失败");

    }

    

}

//

- (void)insertDataWithArray:(NSArray *)array{

    

    if (array.count == 0) {

        //数组为空,没有模型

        return;

    }

    //1.SQL语句  拼接

    NSString *sql = @"insert into Student values(?,?,?)";

    //2.遍历数组

    for (StudentModel *model in array) {

        //

        [_database executeUpdate:sql,model.stuName,model.stuAge,model.stuSex];

        

    }

    

}

 

//

- (void)deleteDataWithArray:(NSArray *)array{

    

    if (array.count == 0) {

        return ;

    }

    

    

    NSString *sql = @"delete from Student where stuName=? and stuAge=? and stuSex=?";

    for (StudentModel *model in array) {

        [_database executeUpdate:sql,model.stuName,model.stuAge,model.stuSex];

 

    }

    

    

}

 

//

- (BOOL)updateDataWithSourceModle:(StudentModel *)sourceModel andNewModel:(StudentModel *)newModel{

    

    if (sourceModel && newModel) {

        //两个模型都不为空时,才执行 数据的修改

        

        NSString *sql = @"update Student set stuName=? , set stuAge=? , set stuSex=? where stuName=? and stuAge=? and stuSex=?";

       BOOL isSuccess = [_database executeUpdate:sql,newModel.stuName,newModel.stuAge,newModel.stuSex,sourceModel.stuName,sourceModel.stuAge,sourceModel.stuSex];

        

        return isSuccess;

 

    }

    return NO;

}

 

 

//

- (NSArray *)selectAllDatas{

    //1.数组,接收模型

    NSMutableArray *dataArr = [NSMutableArray array];

    

    //2.sql 语句

    NSString *sql = @"select *from Student";

    //3.查询

     FMResultSet *set = [_database executeQuery:sql];

    //4.遍历,分装模型

    while ([set next]) {

        NSString *name = [set stringForColumn:@"stuName"];

        NSString *age  = [set stringForColumn:@"stuAge"];

        NSString *sex  = [set stringForColumn:@"stuSex"];

        

        NSDictionary *dic = @{@"stuName":name,@"stuAge":age,@"stuSex":sex};

        

        StudentModel *model = [StudentModel createModelWithDic:dic];

        

        [dataArr addObject:model];

        

    }

    

    return dataArr;

}

//姓名

- (NSArray *)selectForName:(NSString *)name {

    

    if (name.length == 0) {

        return nil;

    }

    NSMutableArray *dataArr = [NSMutableArray array];

    NSString *sql = @"select * form Student where stuName=? ";

    FMResultSet *set = [_database executeQuery:sql,name];

    

    while ([set next]) {

        NSString *name = [set stringForColumn:@"stuName"];

        NSString *age = [set stringForColumn:@"stuAge"];

        NSString *sex = [set stringForColumn:@"stuSex"];

        

        

        NSDictionary *dic = @{@"stuName":name,@"stuAge":age,@"stuSex":sex};

        

        StudentModel *model = [StudentModel createModelWithDic:dic];

        

        [dataArr addObject:model];

    }

    

    return dataArr;

}

FMDB----SQL----数据库