I create a database and put the * .sqlite file in the source folder in Xcode. Before opening the database, I copy it to the document folder from the resources. This works fine on a simulator, but does not work on an iPhone. For some reason, the database file does not exist on the iPhone device. This is my code:
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *databasePath = [documentsDir stringByAppendingPathComponent:databaseFileName];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL success = [fileManager fileExistsAtPath:databasePath];
if(!success)
{
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseFileName];
BOOL fileExists = [fileManager fileExistsAtPath:databasePathFromApp];
if (fileExists) {
[fileManager removeItemAtPath:databasePath error:nil];
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}
[fileManager release];
}
return databasePath;
For some reason, this string returns NO all the time.
BOOL fileExists = [fileManager fileExistsAtPath:databasePathFromApp];
path in database PathFromApp somthing like this
/var/alexander/xxxx-xxx-xxxx-xxx..../AppName.app/database.sqlite
Question: why the database does not exist in resources?
source
share