Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // DATABASE PATH
- #define DATABASE_PATH [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/MYDATABASENAME.sqlite"]
- - (void)createEditableCopyOfDatabaseIfNeeded
- {
- sqlite3 *database;
- NSFileManager *fileManager = [NSFileManager defaultManager];
- NSError *error;
- BOOL success = [fileManager fileExistsAtPath:DATABASE_PATH];
- if (!success) {
- // The writable database does not exist, so copy the default to the appropriate location.
- NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:DATABASE_FILENAME];
- success = [fileManager copyItemAtPath:defaultDBPath toPath:DATABASE_PATH error:&error];
- }
- if (!success) {
- NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
- } else {
- if (sqlite3_open([DATABASE_PATH UTF8String], &database) == SQLITE_OK) {
- XLog(@"DATABASE OPENED");
- XLog(DATABASE_PATH);
- } else {
- NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database));
- }
- }
- sqlite3_close(database);
- }
Add Comment
Please, Sign In to add comment