title: Copy SQLite Database from URL to Local Path
body: <p>What can I use to copy a remote database to a local path?</p>
  
  <pre><code>success = [FileManager copyItemAtURL:dbPath toPath:databasePath error: &amp;error];
  </code></pre>
  
  <p>Obviously the above won't work. Looking at the manual it seems you can copy path to path or url to url. But I've seen example of myapp://path/to/documents/database.sql. Now I'm sorta confused. Can someone possibly point me in the right direction? </p>
  
  <p>This is what I'm doing right now... but it's not overwriting the database or I'm doing things in the wrong order. The file is only a couple of Kbs also.</p>
  
  <pre><code>- (void)createDB {
  BOOL success;
  NSError *error;
  
  NSData *fetchedData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.dot.com/book/book.sqlite"]];
  NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
  NSString *filePath = [documentsPath stringByAppendingPathComponent:@"book.sqlite"];
  [fetchedData writeToFile:filePath atomically:YES];
  NSLog(@"%@", filePath);
  
  NSFileManager *FileManager = [NSFileManager defaultManager];
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectory = [paths objectAtIndex:0];
  NSString *databasePath = [documentsDirectory stringByAppendingString:@"book.sqlite"];
  success = [FileManager fileExistsAtPath:databasePath];
  if(success)return;
  NSString *dbPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"book.sqlite"];
  success = [FileManager copyItemAtPath:dbPath toPath:databasePath error: &amp;error];
  if(!success)
      NSAssert1(0, @"Failed to copy database. Error: %@", [error localizedDescription]);
  }
  </code></pre>