导入#import "FMDatabaseAdditions.h"
//打开数据库
- (BOOL)openDB {
NSString *path = [kDocumentFolder stringByAppendingPathComponent:DATABASE_LOCAL_NAME];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL find = [fileManager fileExistsAtPath:path];
//找到数据库文件并打开
if(find) {
db_ = [FMDatabase databaseWithPath:path];
if (![db_ open]) {
NSLog(@"Could not open db.");
return 0;
}
//判断该字段在表里面是否存在
if (![db_ columnExists:@"imageUrl" inTableWithName:@"SpbCaseTaskImageStatus"]) {
NSString *altertStr=[NSString stringWithFormat:@"ALTER TABLE %@ ADD %@ VARCHAR",@"SpbCaseTaskImageStatus",@"imageUrl"];
BOOL isRec = [db_ executeUpdate:altertStr];
// NSLog(@"%@------",isRec);
}
return 1;
} else { //找不到数据库则创建数据库,初始化数据库结构
db_ = [FMDatabase databaseWithPath:path];
BOOL isCreate = 0;
if ([db_ open]) {
isCreate = [self createTable];
}
//创建数据表不成功或者不能打开,则删除创建的文件(如果有)
if (!isCreate || ![db_ open]) {
find = [fileManager fileExistsAtPath:path];
if(find) {
[fileManager removeItemAtPath:path error:nil];
}
NSLog(@"Could not open db.");
return 0;
}
return 1;
}
return 0;
}