Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 255
Yaplex Avatar asked Jan 01 '26 12:01

Yaplex


1 Answers

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

like image 147
Yaplex Avatar answered Jan 03 '26 03:01

Yaplex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!