我们使用FMDB 时,添加数据,时时需要手动添加属性,值,当属性很多时,会过于麻烦
FMDB 有
- (BOOL)executeUpdate:(NSString*)sql withArgumentsInArray:(NSArray *)arguments {
return [self executeUpdate:sql error:nil withArgumentsInArray:arguments orDictionary:nil orVAList:nil];
}
- (BOOL)executeUpdate:(NSString*)sql withParameterDictionary:(NSDictionary *)arguments {
return [self executeUpdate:sql error:nil withArgumentsInArray:nil orDictionary:arguments orVAList:nil];
方法,我哦们可以将需要添加的属性,值,做成Key--Value 动态添加,因此需要封装一个方法
// 返回格式:insert into table_name (c1,c2,c3) values (:a,:b,:c);
-(NSString*) assembleInsertSql:(NSDictionary*) tableName
{
NSArray *datas = [tableName objectForKey:@"data"];
if([datas count] == 0){
return nil;
}
NSDictionary *record = [datas objectAtIndex:0];// 取出第一条记录,为了拿到列名
NSArray *columns = [record allKeys];
NSString *table = [tableName objectForKey:@"tableName"];
NSString *prefix = [NSString stringWithFormat:@"insert into %@ (", table];
NSMutableString *middle = [NSMutableString new];
for(int i=0;i<[columns count];i++){
NSString *columnName = [columns objectAtIndex:i];// 列名
if(![@"_id" isEqualToString:columnName]){
[middle appendString:columnName];
[middle appendString:@","];
}
}
NSString* cuttedMiddle = [middle substringToIndex:middle.length - 1];
NSMutableString *suffix = [NSMutableString new];
[suffix appendString:@") values ("];
for(int i=0;i<[columns count];i++){
NSString *columnName = [columns objectAtIndex:i];// 列名
if(![@"_id" isEqualToString:columnName]){
[suffix appendString:@":"];
[suffix appendString:columnName];
[suffix appendString:@","];
}
}
NSString *cuttedSuffix = [suffix substringToIndex:suffix.length - 1];
NSMutableString *sql = [NSMutableString new];
[sql appendString:prefix];
[sql appendString:cuttedMiddle];
[sql appendString:cuttedSuffix];
[sql appendString:@");"];
return sql;
}
//处理数据
-(void) handleJsonObject:(NSDictionary*) tableName
{
[db1 open];
NSString *createC = @"CREATE TABLE Class (Name text,Age integer,Sex text)";
BOOL e = [db1 executeUpdate:createC];
if (e) {
NSLog(@"创建表成功");
}else
{
NSLog(@"创建失败");
}
NSString *sql = [self assembleInsertSql:tableName];// 拼装sql语句:insert into table_name (c1,c2,c3) values (:a,:b,:c);
if(!sql){
return;// 说明data里无数据,不需要操作数据库
}
NSArray *datas = [tableName objectForKey:@"data"];
for(NSDictionary *data in datas){
NSMutableDictionary *mutable = [NSMutableDictionary dictionaryWithDictionary:data];
[mutable removeObjectForKey:@"_id"];
BOOL result = [db1 executeUpdate:sql withParameterDictionary:mutable];
if(result){
NSLog([NSString stringWithFormat:@"%d", [db1 lastErrorCode]], nil);
NSLog([db1 lastErrorMessage], nil);
}
}
[db close];
}
FMDB 有
- (BOOL)executeUpdate:(NSString*)sql withArgumentsInArray:(NSArray *)arguments {
return [self executeUpdate:sql error:nil withArgumentsInArray:arguments orDictionary:nil orVAList:nil];
}
- (BOOL)executeUpdate:(NSString*)sql withParameterDictionary:(NSDictionary *)arguments {
return [self executeUpdate:sql error:nil withArgumentsInArray:nil orDictionary:arguments orVAList:nil];
方法,我哦们可以将需要添加的属性,值,做成Key--Value 动态添加,因此需要封装一个方法
// 返回格式:insert into table_name (c1,c2,c3) values (:a,:b,:c);
-(NSString*) assembleInsertSql:(NSDictionary*) tableName
{
NSArray *datas = [tableName objectForKey:@"data"];
if([datas count] == 0){
return nil;
}
NSDictionary *record = [datas objectAtIndex:0];// 取出第一条记录,为了拿到列名
NSArray *columns = [record allKeys];
NSString *table = [tableName objectForKey:@"tableName"];
NSString *prefix = [NSString stringWithFormat:@"insert into %@ (", table];
NSMutableString *middle = [NSMutableString new];
for(int i=0;i<[columns count];i++){
NSString *columnName = [columns objectAtIndex:i];// 列名
if(![@"_id" isEqualToString:columnName]){
[middle appendString:columnName];
[middle appendString:@","];
}
}
NSString* cuttedMiddle = [middle substringToIndex:middle.length - 1];
NSMutableString *suffix = [NSMutableString new];
[suffix appendString:@") values ("];
for(int i=0;i<[columns count];i++){
NSString *columnName = [columns objectAtIndex:i];// 列名
if(![@"_id" isEqualToString:columnName]){
[suffix appendString:@":"];
[suffix appendString:columnName];
[suffix appendString:@","];
}
}
NSString *cuttedSuffix = [suffix substringToIndex:suffix.length - 1];
NSMutableString *sql = [NSMutableString new];
[sql appendString:prefix];
[sql appendString:cuttedMiddle];
[sql appendString:cuttedSuffix];
[sql appendString:@");"];
return sql;
}
//处理数据
-(void) handleJsonObject:(NSDictionary*) tableName
{
[db1 open];
NSString *createC = @"CREATE TABLE Class (Name text,Age integer,Sex text)";
BOOL e = [db1 executeUpdate:createC];
if (e) {
NSLog(@"创建表成功");
}else
{
NSLog(@"创建失败");
}
NSString *sql = [self assembleInsertSql:tableName];// 拼装sql语句:insert into table_name (c1,c2,c3) values (:a,:b,:c);
if(!sql){
return;// 说明data里无数据,不需要操作数据库
}
NSArray *datas = [tableName objectForKey:@"data"];
for(NSDictionary *data in datas){
NSMutableDictionary *mutable = [NSMutableDictionary dictionaryWithDictionary:data];
[mutable removeObjectForKey:@"_id"];
BOOL result = [db1 executeUpdate:sql withParameterDictionary:mutable];
if(result){
NSLog([NSString stringWithFormat:@"%d", [db1 lastErrorCode]], nil);
NSLog([db1 lastErrorMessage], nil);
}
}
[db close];
}