for (int i=1; i<arr.count; i ++) {
NSString * schame = [diclistData objectForKey:[arr objectAtIndex:i]];
NSString * table = [arr objectAtIndex:i];
NSString *successDropTable = [[NSUserDefaults standardUserDefaults] objectForKey:
[NSString stringWithFormat:@"%@%d_%@_successDrop",App_Version,enterpriseno,table]];
if (!successDropTable ) {
successDropTable = @"";
}
NSLog(@"the successDropTable :%@_%di:%d",successDropTable,enterpriseno,i);
// if ([successDropTable isEqualToString:@"yes"]) {
// continue;
// }
// else{
[DBConnection updateTheTableStructureWith:table WithSchame:schame success:^(BOOL issuccess) {
if (issuccess) {
NSLog(@"creattable success");
[[NSUserDefaults standardUserDefaults] setObject:@"yes" forKey:[NSString stringWithFormat:@"%@%d_%@_successDrop",App_Version,enterpriseno,table]];
}
} failure:^(NSError *error) {
}];
// }
}
for里面的if判断如果注释掉,直接执行创建表的方法,则可以成功新建表,反之加if判断,同样执行了if里面的方法,新建表不成功(有输出creattable success,实际数据库里没有新表)。。。
fmdb的建表方法:
+ (void)updateTheTableStructureWith:(NSString *)table
WithSchame:(NSString *)schame
success:(void(^)(BOOL issuccess))success
failure:(void(^)(NSError *error))failure{
FMDatabase *db = [DBConnection getSharedDatabase];
[db open];
if ([db tableExists:table]){
NSString *sql = [NSString stringWithFormat:@"DROP TABLE IF EXISTS %@ ",table];
BOOL dropsuccess = [db executeUpdate:sql];
NSLog(@"Error %d : %@",[db lastErrorCode],[db lastErrorMessage]);
if (dropsuccess) {
NSLog(@"DROP tablbe secceuful->-> %@",table);
NSLog(@"Error %d : %@",[db lastErrorCode],[db lastErrorMessage]);
}
NSString *creatTablesql = [NSString stringWithFormat:@"%@",schame];
BOOL creatsuccess = [db executeUpdate:creatTablesql];
//if (creatsuccess) {
NSLog(@"update Error %d : %@",[db lastErrorCode],[db lastErrorMessage]);
NSLog(@"Creat tablbe secceuful ->->%@-->%@",table,schame);
success(creatsuccess);
// }
}
else{
NSString *creatTablesql = [NSString stringWithFormat:@"%@",schame];
BOOL csuccess = [db executeUpdate:creatTablesql];
// if (csuccess) {
NSLog(@"update Error %d : %@",[db lastErrorCode],[db lastErrorMessage]);
NSLog(@"Creat tablbe secceuful ->->%@-->%@",table,schame);
// }
success(csuccess);
}
[db close];
}