簡體   English   中英

如何在Cocoa的Application Support目錄中復制數據庫文件而不丟失數據?

[英]How to copy database file in Application Support directory in Cocoa without losing data?

我嘗試過這段代碼,但它在文檔目錄中創建了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]);

    }
}

如何確保在復制數據庫文件時不會創建空數據庫?

任何幫助將不勝感激。

首先,您的代碼不是嘗試創建數據庫文件。

那么......您只想將數據庫文件“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返回的目錄不保證存在; 如有必要,您需要自己創建它們(請參閱文檔 )。 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]);

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM