如何为sqlite3_open指定工作目录

时间:2011-06-30 19:49:17

标签: xcode sqlite

在xCode中工作一切正常如果我通过在工作目录中双击项目而不是从xCode本身执行构建和运行来执行。然后找不到正确的数据库。

如何修改......

 sqlite3_open("Airports.sqlite", &db);

所以它可以在当前工作目录中找到Airports.sqlite吗?

1 个答案:

答案 0 :(得分:0)

将数据库复制到应用程序目录,之后可以使用数据库:)

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
        NSString *dbPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Airports.sqlite"];

BOOL success = [fileManager fileExistsAtPath:dbPath];

if (!succes){

    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Airports.sqlite"];
                BOOL success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];

                if (!success) 
                    NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
                else
                    NSLog(@"Succesfully created writable database");
}