【问题标题】:How to copy database file in Application Support directory in Cocoa without losing data?如何在 Cocoa 的 Application Support 目录中复制数据库文件而不丢失数据?
【发布时间】:2016-08-07 03:26:28
【问题描述】:

我已经尝试过这段代码,但它在文档目录中创建了 BLANK database.sqlite 文件。

- (void)createEditableCopyOfDatabaseIfNeeded
{
    // First, test for existence.
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"prediction.sqlite"];



    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:@"prediction.sqlite"];

    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];


    if (!success) {
        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);

    }
}

如何确保在复制数据库文件时不会创建空数据库?

任何帮助将不胜感激。

【问题讨论】:

  • 该代码从不尝试创建数据库文件。
  • 但是我只是将数据库文件从 NSbundle 复制到文档目录。
  • 不,你根本没有这样做。
  • 为什么?但无论如何我都想复制文件。
  • OK 那么哪个语句在复制文件?

标签: objective-c macos sqlite cocoa osx-yosemite


【解决方案1】:

首先,您的代码没有尝试创建数据库文件。

所以...您只想将数据库文件“prediction.sqlite”从源包复制到 UserDomain 目录? 如果这是你的目的,你应该注意代码:

  if (success) return;
// The writable database does not exist, so copy the default to the appropriate location.

正确的应该是:

      if (!success) return;
// The writable database does not exist, so copy the default to the appropriate location.

NSSearchPathForDirectoriesInDomains返回的目录不保证存在;如有必要,您需要自己创建它们(请参阅documentation)。 NSFileManager 的createDirectoryAtPath:withIntermediateDirectories:attributes:error: 可以提供帮助。@Anomie

尝试这些代码:(在复制“prediction.sqlite”文件之前,您需要将该文件添加到您的项目包中。):

- (void)createEditableCopyOfDatabaseIfNeeded
{
    // First, test for existence.
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"prediction.sqlite"];

    success = [fileManager fileExistsAtPath:writableDBPath];

    if (!success){
        // create the directories, because the directories returned by NSSearchPathForDirectoriesInDomains are not guaranteed to exist
        if (![fileManager createFileAtPath:writableDBPath contents:nil attributes:nil]) {
            return;
        }
    }

    // The writable database does not exist, so copy the default to the appropriate location.
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"prediction.sqlite"];

    // remove the same file, before you copy the file
    [fileManager removeItemAtPath:writableDBPath error:nil];

    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];

    if (!success) {
        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);

    }
}

【讨论】:

  • 执行上述方法后问题没有解决,它复制没有表格的空白数据库文件。
  • 为什么我可以用表复制数据库......我已经更新了答案,看看它是否可以帮助你
  • @Stoll 此代码复制数据库结构,但不复制数据。
猜你喜欢
  • 2015-03-23
  • 2017-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多