SQLite database on iPhone and Simulator
I create database and put *.sqlite file in recources folder in Xcode. Before opening database I copy it to documents folder from resources. This works fine on simulator, but does not work on iPhone. For some reasons database file does not exists in 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) // all the time it's YES, even if I delete database
{
// copy database from application folder
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseFileName];
BOOL fileExists = [fileManager fileExistsAtPath:databasePathFromApp];
if (fileExists) { // It's NO, for some reasons database does not exist
[fileManager removeItemAtPath:databasePath error:nil];
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}
[fileManager release];
}
return databasePath;
For some reasons this line returns NO all the time
BOOL fileExists = [fileManager fileExistsAtPath:databasePathFromApp];
the path in databasePathFromApp somthing like this
/var/alexander/xxx-xxx-xxx-xxx..../AppName.ap开发者_开发知识库p/database.sqlite
The question is: why database does not exists in recources?
Finaly found the problem. iPhone device care about file name register, so "database.sqlite" and "DataBase.sqlite" is different files for device, but the same files for simulator. It's so weird.
I posed it in the blog Can't open file in iPhone device, but can in iPhone simulator
Delete database from resource & add it once again & check box appears at top with Copy items into destination group folder.
Clean targets & reset simulator.
精彩评论