Search code examples
iphoneiosobjective-cnsfilemanager

createFileAtPath:contents:attributes: gives "No such file or directory" error


I am trying to write a simple createDB function. If the DB file does not exist I want to create it and proceed. But I'm getting a "No Such file or directory" error while trying to create the file.

Following is code snippet:

-(void) createDB
{
    NSString* docsDir;
    NSArray* dirPaths;

    // Get directory path
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);    
    docsDir = [dirPaths objectAtIndex:0];
    dbPath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent:@"players.db"]];
    NSFileManager* fileMgr = [NSFileManager defaultManager];


    if([fileMgr fileExistsAtPath:dbPath] == NO)
    {
        //Create the file in main application folder
        //dbPath = [[NSBundle mainBundle] pathForResource:@"players" ofType:@".db"];

        if (![fileMgr createFileAtPath:dbPath contents:nil attributes:nil])
            NSLog(@"Error was code: %d - message: %s", errno, strerror(errno));
        // Above NSLog Prints : Error was code: 2 - message: No such file or directory

        NSLog(@"DB PAth : %@",dbPath);
        //Prints "DB Path : /Users/myname/Library/Application Support/iPhone Simulator/6.0/Applications/016E0BD6-E615-4031-A9E4-42E42D863ECB/Library/Documentation/"


        if(sqlite3_open_v2([dbPath UTF8String], &playerDB, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL) == SQLITE_OK)
        {
            char* errorMsg;
            const char* sql_stmt = "CREATE TABLE IF NOT EXISTS PLAYERS (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, GAME TEXT, GAME_DESC TEXT, DOB TEXT, IMAGE BLOB);";
            if (sqlite3_exec(playerDB, sql_stmt, nil, nil, &errorMsg) != SQLITE_OK)
            {
                NSLog(@"%s Create Table '%s' (%1d)", __FUNCTION__, sqlite3_errmsg(playerDB), sqlite3_errcode(playerDB));
            }
        }
        else
            NSLog(@"%s Open DB '%s' (%1d)", __FUNCTION__, sqlite3_errmsg(playerDB), sqlite3_errcode(playerDB));
    }
}

Please let me know if anybody has any input.


Solution

  • replace NSDocumentationDirectory' by

    NSDocumentDirectory
    

    i think it should be

    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);