SQLite creating blank DB at startup

Discussion in 'iOS Programming' started by markredson, Feb 27, 2009.

  1. markredson macrumors newbie

    Feb 27, 2009
    I am building an app that DID work fine. But now when is installed on simulator OR iPhone, a database file is automatically added in the documents folder, therefor it does not copy the database I have provided (with the proper tables) The auto generated database file has nothing in it, so when I try to preform a query, the app fails.

    Code: (which is copied right from sqlitebooks except for the DB name)
        // First, test for existence.
        BOOL success;
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSError *error;
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"db.sqlite"];
        success = [fileManager fileExistsAtPath:writableDBPath];
        if (success) return;
        // The writable database does not exist, so copy the default to the appropriate location.
        NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"db.sqllite"];
        success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
        if (!success) {
            NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    Interestingly enough, I think the command that checks for the file is creating the file... no matter what I name it that file shows up somehow.


Share This Page