2 daffodilgirl DaffodilGirl 于 2013.01.22 15:28 提问

创建文件:内容:属性,给出错误

我需要一个简单的createDB函数,如果DB文件不错字,就创建生成,但是在创建文件时报出 No Such file or directory 错误。

下面是代码中的一段:

-(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));
    }
}

不知道哪里出错了。

1个回答

redCoral_
redCoral_   2013.01.22 16:20

别用NSDocumentationDirectory

改为:NSDocumentDirectory

改完之后应该是这样:

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
Csdn user default icon
上传中...
上传图片
插入图片