Hoping someone can help a new iOS developer out – I have a ready made sqlite database called teamresults.db that I added to the project using “Add Files…” but when I’m using the below code I seem to be connecting/referencing an empty database instead of the one that’s added (on inspection through the terminal the database the simulator uses is 0mb in size where as the one I’ve added is around 3mb).
It seems like the application is creating an empty database but I can’t work out why? Any thoughts?
For reference I don’t get any error messages:
BOOL success;
dbName = @"teamresults.db";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
dbPath = [documentsDir stringByAppendingPathComponent:dbName];
NSFileManager *fileManager = [NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:dbPath];
if(success) {
return;
}
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:dbName];
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePathFromApp error:nil];
There seems to be something wrong with this line of code:
The correct one would be:
You should copy database to sandbox path instead of resources path !!