3

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.app/database.sqlite

The question is: why database does not exists in recources?

Yaplex
  • 2,272
  • 1
  • 20
  • 38
  • The "databaseFileName" and the name of the sqlite file in your bundle are same? –  Jun 09 '11 at 14:20

2 Answers2

3

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

Yaplex
  • 2,272
  • 1
  • 20
  • 38
0

Delete database from resource & add it once again & check box appears at top with Copy items into destination group folder.

Clean targets & reset simulator.

iAmitWagh
  • 453
  • 2
  • 13
  • no, it works fine at simulator. The question is why database does not exist in Resources in iPhone, but do exists on simulator – Yaplex Jun 09 '11 at 14:20