How to specify working directory for sqlite3_open
Working in xCode everything works OK if I execute by double-clicking the project down in the working directory but not from xCode itself doing a Build and Run. Then the database isn't being found correctly.
开发者_Python百科How do I modify...
sqlite3_open("Airports.sqlite", &db);
so it can find Airports.sqlite in the current working directory?
Copy your database to your applications directory and after that you can use your database :)
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");
}
精彩评论