Guest User

Untitled

a guest
Jun 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. // DATABASE PATH
  2. #define DATABASE_PATH [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/MYDATABASENAME.sqlite"]
  3.  
  4.  
  5. - (void)createEditableCopyOfDatabaseIfNeeded
  6. {
  7. sqlite3 *database;
  8. NSFileManager *fileManager = [NSFileManager defaultManager];
  9. NSError *error;
  10. BOOL success = [fileManager fileExistsAtPath:DATABASE_PATH];
  11. if (!success) {
  12. // The writable database does not exist, so copy the default to the appropriate location.
  13. NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:DATABASE_FILENAME];
  14. success = [fileManager copyItemAtPath:defaultDBPath toPath:DATABASE_PATH error:&error];
  15. }
  16. if (!success) {
  17. NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
  18. } else {
  19. if (sqlite3_open([DATABASE_PATH UTF8String], &database) == SQLITE_OK) {
  20. XLog(@"DATABASE OPENED");
  21. XLog(DATABASE_PATH);
  22. } else {
  23. NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database));
  24. }
  25. }
  26. sqlite3_close(database);
  27. }
Add Comment
Please, Sign In to add comment