0

I wan't to add a sqlite file to preload data for my app. I created this file and copied it into the project directory. I added it to Copy Bundle Resources and copy files. But when I wan't to use this file I get an error saying that there is no such file. I would appreciate if anyone could help me. The code is listed below:

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); } 

The 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"}

I tried to change my code:

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); } 

But I get another 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

And another variant:

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]; 
1
  • Since your new to sqlite and don't have set up everything already, may I give you a little tip to ease your next days. Check out Realm . Commented Jul 29, 2015 at 13:41

2 Answers 2

2

Use the following to get the paths to the file in question:

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

You can then copy the file with the following:

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

I would include more checking and validation than the above - in particular the fileExistsAtPath: method in NSFileManager.

Sign up to request clarification or add additional context in comments.

2 Comments

But I get the same error saying that there is no such file
Sorry, am I doing this properly? 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];
1

I hope, this gonna help:

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"); } 

Two things you can check for, go to your document directory of application, check if you can go there and other thing is make sure your sqlite file is properly added to project, you can delete and add again. Let me know, if still there is issue

5 Comments

I'm not sure if I'm doing it properly. I changed the code. I'll add it to the question
In your error: "/Users/user/Library/Developer/CoreSimulator/Devices/DA5D105A-C9AF-4C91-80B3-DFF2C157CC92/data/Containers/Data/Application/BEED2AB9-BFDA-40AC-A8D0-55DD81F9D985/Library/DocumentationCookBookData.sqlite" what is it? its not a right path. You are not getting right path for document directory. What you are getting in log?
I have edited answer. Comment everything what you have done for once and write this and then what you get in log?
Sorry, but how should I use it to preload data into the store sqlite file for my core data model?
The problem appeared to be connected with the project and file itself. Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.