我的应用程序有一个SQLite数据库。我不知道如何解释,但有时sqlite文件中的所有表都会被删除。它不会经常发生,但它会发生。 .db文件仍然存在,但没有表。
#pragma mark LOCAL DATABASE
-(void)createEditableCopyOfDatabaseIfNeeded {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"Spread.sqlite"];
BOOL success = [fileManager fileExistsAtPath:writableDBPath];
if (success) {
return;
}
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Spread.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
} else {
NSLog(@"Spread - DB Created");
}
}
+(FMDatabase *) getDatabase {
static FMDatabase *db;
if (!db) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"Spread.sqlite"];
FMDatabase *db = [FMDatabase databaseWithPath:writableDBPath];
[db open];
return db;
} else {
return db;
}
}