添加sqlite数据库:没有那个文件或目录

Adding sqlite database: No such file or directory

我不想添加 sqlite 文件来为我的应用程序预加载数据。我创建了这个文件并将其复制到项目目录中。我将它添加到 Copy Bundle Resources 并复制文件。但是当我不想使用这个文件时,我得到一个错误,说没有这样的文件。如果有人能帮助我,我将不胜感激。 代码如下:

NSURL *preloadURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"CookBookData" ofType:@"sqlite"]] ;

if (![[NSFileManager defaultManager] copyItemAtURL:preloadURL toURL:storeURL error:&error]) {
    NSLog(@"Couldn't preload data, error: %@", error);
}

错误:

2015-07-29 15:42:34.342 CookBook v1[2897:24912] Couldn't preload data, error: Error Domain=NSCocoaErrorDomain Code=4 "The operation couldn’t be completed. (Cocoa error 4.)" UserInfo=0x7fe4a35275d0 {NSSourceFilePathErrorKey=/Users/user/Library/Developer/CoreSimulator/Devices/.../data/Containers/Bundle/Application/.../CookBook v1.app/CookBookData.sqlite, NSUserStringVariant=( Copy ), NSDestinationFilePath=/Users/user/Library/Developer/CoreSimulator/Devices/.../Documents/CookBookModel 2.sqlite, NSFilePath=/Users/user/Library/Developer/CoreSimulator/Devices/.../data/Containers/Bundle/Application/.../CookBook v1.app/CookBookData.sqlite, NSUnderlyingError=0x7fe4a35145d0 "The operation couldn’t be completed. No such file or directory"}

我尝试更改我的代码:

NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CookBookModel3.sqlite"];
NSError *error = nil;

NSString *storePath = [storeURL path];
NSLog(@"Error %@", error);

NSString *dbName = @"CookBookData.sqlite";
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *dbPath = [documentDirectory stringByAppendingString:dbName];

BOOL success = [fileManager fileExistsAtPath:dbPath];
if (!success) {
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:dbName];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
    if (success) {
        NSLog(@"Success");
    }
    else{
        NSLog(@"Error %@", [error localizedDescription]);
    }
}
else {
    NSLog(@"Already exists");
}

if (![[NSFileManager defaultManager] copyItemAtPath:dbPath toPath:storePath error:&error]) {
    NSLog(@"Couldn't preload data, error: %@", error);
}

但我收到另一个错误:

2015-07-30 12:12:29.118 CookBook[29209:208802] Couldn't preload data, error: Error Domain=NSCocoaErrorDomain Code=516 "The operation couldn’t be completed. (Cocoa error 516.)" UserInfo=0x7fe9526318b0 {NSSourceFilePathErrorKey=/Users/user/Library/Developer/CoreSimulator/Devices/DA5D105A-C9AF-4C91-80B3-DFF2C157CC92/data/Containers/Data/Application/BEED2AB9-BFDA-40AC-A8D0-55DD81F9D985/Library/DocumentationCookBookData.sqlite, NSUserStringVariant=( Copy ), NSDestinationFilePath=/Users/user/Library/Developer/CoreSimulator/Devices/DA5D105A-C9AF-4C91-80B3-DFF2C157CC92/data/Containers/Data/Application/BEED2AB9-BFDA-40AC-A8D0-55DD81F9D985/Documents/CookBookModel3.sqlite, NSFilePath=/Users/user/Library/Developer/CoreSimulator/Devices/DA5D105A-C9AF-4C91-80B3-DFF2C157CC92/data/Containers/Data/Application/BEED2AB9-BFDA-40AC-A8D0-55DD81F9D985/Library/DocumentationCookBookData.sqlite, NSUnderlyingError=0x7fe952610c30 "The operation couldn’t be completed. File exists"} 2015-07-30 12:12:29.121 CookBook[29209:208802] Managed object context

还有另一个变体:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *path = [documentsDir stringByAppendingPathComponent:@"CookBookData.sqlite"]; 
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager copyItemAtPath:path toPath:storePath error:&error];

使用以下方法获取相关文件的路径:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *path = [documentsDir stringByAppendingPathComponent:@"CookBookData.sqlite"];

然后您可以使用以下命令复制文件:

NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"database.sqlite"];
[fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];

我会包括比上面更多的检查和验证 - 特别是 NSFileManager 中的 fileExistsAtPath: 方法。

我希望,这会有所帮助:

NSError *error;
NSString *dbName = @"CookBookData.sqlite";
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *dbPath = [documentDirectory stringByAppendingString:dbName];
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:dbName];

if ([fileManager fileExistsAtPath:defaultDBPath]) { //check if file exists in NSBundle

    BOOL success = [fileManager fileExistsAtPath:dbPath]; // check if exists in document directory
    if (!success) {

        success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error]; //copy to document directory
        if (success) {
            NSLog(@"Database successfully copied to document directory");
        }
        else{
            NSLog(@"Error %@", [error localizedDescription]);
        }
    }
    else {
        NSLog(@"Already exists in document directory");
    }

}
else{
    NSLog(@"Does not exists in NSBundle");
}

您可以检查两件事,转到您的应用程序文档目录,检查您是否可以去那里,另一件事是确保您的 sqlite 文件已正确添加到项目中,您可以删除并重新添加。如果还有问题,请告诉我