FMDB的再次封装
插入命令
/**
增删改查中 除了查询(executeQuery),其余操作都用(executeUpdate)
//1.sql语句中跟columnname 绑定的value 用 ?表示,不加‘’,可选参数是对象类型如:NSString,不是基本数据结构类型如:int,方法自动匹配对象类型
- (BOOL)executeUpdate:(NSString*)sql, ...;
//2.sql语句中跟columnname 绑定的value 用%@/%d表示,不加‘’
- (BOOL)executeUpdateWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);
//3.sql语句中跟columnname 绑定的value 用 ?表示的地方依次用 (NSArray *)arguments 对应的数据替代
- (BOOL)executeUpdate:(NSString*)sql withArgumentsInArray:(NSArray *)arguments;
//4.同3 ,区别在于多一个error指针,记录更新失败
- (BOOL)executeUpdate:(NSString*)sql values:(NSArray * _Nullable)values error:(NSError * _Nullable __autoreleasing *)error;
//5.同3,区别在于用 ? 表示的地方依次用(NSDictionary *)arguments中对应的数据替代
- (BOOL)executeUpdate:(NSString*)sql withParameterDictionary:(NSDictionary *)arguments;
- (BOOL)executeUpdate:(NSString*)sql withVAList: (va_list)args;
*///0.直接sql语句// BOOL result = [db executeUpdate:@"insert into 't_student' (ID,name,phone,score) values(110,'x1','11',83)"];//1.// BOOL result = [db executeUpdate:@"insert into 't_student'(ID,name,phone,score) values(?,?,?,?)",@111,@"x2",@"12",@23];//2.// BOOL result = [db executeUpdateWithFormat:@"insert into 't_student' (ID,name,phone,score) values(%d,%@,%@,%d)",112,@"x3",@"13",43];
2. 删除命令
///0.直接sql语句// BOOL result = [db executeUpdate:@"delete from 't_student' where ID = 110"];//
1.// BOOL result = [db executeUpdate:@"delete from 't_student' where ID = ?",@(111)];//
2.// BOOL result = [db executeUpdateWithFormat:@"delete from 't_student' where ID = %d",112];//
3.BOOLresult = [db executeUpdate:@"delete from 't_student' where ID = ?"withArgumentsInArray:@[@113]];
3.更新命令
//0.直接sql语句//
BOOL result = [db executeUpdate:@"update 't_student' set name = 110 where ID = 'x1'"];//
1.// BOOL result = [db executeUpdate:@"update 't_student' set name = ? where ID = ?",@111,@"x2" ];//
//假如要更新多条的话需要用逗号分隔, where后面是索引
2.// BOOL result = [db executeUpdateWithFormat:@"update 't_student' set name = %d,sex = ? age = ? where ID = %@",113,@"男",@"20", @"x3" ];//
3.BOOLresult = [db executeUpdate:@"update 't_student' set name= ? where ID = ?"withArgumentsInArray:@[@113,@"x3"]];
4.查询命令
/**
FMResultSet根据column name获取对应数据的方法
intForColumn:
longForColumn:
longLongIntForColumn:
boolForColumn:
doubleForColumn:
stringForColumn:
dataForColumn:
dataNoCopyForColumn:
UTF8StringForColumnIndex:
objectForColumn:
*/
//0.直接sql语句// FMResultSet *result = [db executeQuery:@"select * from 't_student' where ID = 110"];//
1.// FMResultSet *result = [db executeQuery:@"select *from 't_student' where ID = ?",@111];//
2.// FMResultSet *result = [db executeQueryWithFormat:@"select * from 't_student' where ID = %d",112];//
3.FMResultSet *result = [db executeQuery:@"select * from 't_student' where ID = ?"withArgumentsInArray:@[@113]];//
4// FMResultSet *result = [db executeQuery:@"select * from 't_sutdent' where ID = ?" withParameterDictionary:@{@"ID":@114}];
NSMutableArray*arr = [NSMutableArrayarray];
while([result next]) {
PersonVO *person = [PersonVO new];
person.ID = [result intForColumn:@"ID"];
person.name = [result stringForColumn:@"name"];
person.phone = [result stringForColumn:@"phone"]; person.score = [result intForColumn:@"score"];
[arr addObject:person];
NSLog(@"从数据库查询到的人员 %@",person.name);