iOS下载sqlite数据库并替换现有的数据库 [英] iOS download sqlite database and replace existing one

查看:140
本文介绍了iOS下载sqlite数据库并替换现有的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须下载数据库并替换沙盒中现有的数据库:
这是可行的方法:

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


推荐答案

请考虑以下步骤:

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

来自 iphonedevsdk论坛主题。

这篇关于iOS下载sqlite数据库并替换现有的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆