createEditableCopyOfDatabaseIfNeeded创建数据库的空复制!(cre

2019-09-28 13:02发布

我使用的是“著名的” :)功能createEditableCopyOfDatabaseIfNeeded用于在iPhone应用程序创建我的数据库的可编辑副本。该功能复制的文件目录中的原始数据库只有在有没有数据库的副本..很简单..但现在我遇到了麻烦,因为我不知道为什么,但与SDK 3.2.3在iOS 4.1的功能复制我的数据库....空!

下面是代码:

- (void)createEditableCopyOfDatabaseIfNeeded  
{ 
    // First, test for existence. 
    BOOL success; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSError *error; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"funghi.sql"]; 
    success = [fileManager fileExistsAtPath:writableDBPath]; 
    if (success) return; 
    // The writable database does not exist, so copy the default to the appropriate location. 
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"funghi.sql"]; 
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error]; 
    if (!success) { 
        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]); 
    }
}

该数据库是完全正确! 我可以用Firefox的插件sqlite的阅读原文分贝! 任何帮助吗?

多谢!!!

Answer 1:

仅用于测试目的:

尝试先删除现有的数据库。 我猜,当你在写代码和调试你运行你的应用程序这个程序之前存在。 SQLite的就已经创建了一个空数据库为您服务。



文章来源: createEditableCopyOfDatabaseIfNeeded create an empty copy of db!