我正在编写一个重置函数,目的是将我的SQLite数据库恢复到初始状态,并填入一些预加载数据。下面的代码从文件夹中删除数据库并从绑定中复制数据库,但仍然显示来自旧数据库的数据?
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"MoneyMonthly.sqlite"];
if([[NSFileManager defaultManager] fileExistsAtPath:filePath]){
[[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
}
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager]; NSError *error;
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"MoneyMonthly.sqlite"];
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"MoneyMonthly.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}