I’m learning Objective-C, so please, be nice. 🙂
While learning how to connect Objective-C and a SQLite db, I found some authors suggesting to use this method (or a very similar one) before starting querying the db:
-(void) checkAndCreateDatabase{
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:databasePath];
if(success) return;
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
[fileManager release];
}
Which is the main advantage of using this code over using NSString *dbPath = [[NSBundle mainBundle] pathForResource:@"myDb" ofType:@"sqlite"] ?
In iOS you can not write to the app bundle so the database is usually copied to the apps sandbox area.
The following code is fine to get the path to your database in the app bundle:
The document directory path can be found with this code:
There are several other directories you can search for, lookup the enumeration:
NSSearchPathDirectory.