0

I have to download the database and replace existing one in the sandbox: Here's is the presumable way to do that:

DBHelpers *help=[[DBHelpers alloc] init];


    NSString *targetPath=[[help DocumentsDirectory] stringByAppendingPathComponent:DATABASE_NAME];


    NSLog(@"Target path: %@", targetPath);
    NSFileManager *fileManager=[NSFileManager defaultManager];

    //if([fileManager fileExistsAtPath:targetPath])
    //{
      //  NSLog(@"Exists");
       // return;
    }
    NSString *sourcePath=[help PathForResource:DATABASE_NAME];
    NSLog(@"SourcePath path: %@", sourcePath);
    NSError *error;
  NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:@"www.hello.com/mydb.sqlite"]];
    [data writeToFile:targetPath atomically:NO];

//  [fileManager copyItemAtPath:sourcePath toPath:targetPath error:&error];
    NSLog(@"Error %@", error);
Vladimir Stazhilov
  • 1,956
  • 4
  • 31
  • 63

1 Answers1

5

Consider these steps:

NSData *fetchedData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.myserver.com/files/DBName.sqlite"]]];
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDire ctory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"DBName.sqlite"];
[fetchedData writeToFile:filePath atomically:YES];

from iphonedevsdk forum thread.

p.campbell
  • 98,673
  • 67
  • 256
  • 322
  • This no longer seems to work. I'm doing essentially the same thing in [this question](http://stackoverflow.com/questions/16150196/updating-sqlite-database-without-xml), but I'm getting an error saying the database I'm downloading "does not appear to be a SQLite database" when I restart my app. – GeneralMike Apr 26 '13 at 17:52