iOS download sqlite database and replace existing one

爱⌒轻易说出口 提交于 2019-12-06 14:58:33

问题


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);

回答1:


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.



来源:https://stackoverflow.com/questions/9132684/ios-download-sqlite-database-and-replace-existing-one

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!