开发者

how to download update from iphone app to replace old sqlite database

i use sqlite database on m开发者_Python百科y iphone app and

i need to update this database from the internet from my server

how i can download the new database and delete the old database

and recopy the new database to document directory


  1. Download your file with +[NSData dataWithContentsOfURL:].
  2. Close sqlite DB, if opened.
  3. Remove old DB file (may be just rename - if something went wrong with downloaded data, you may revert to prev. version of a DB faile) with -[NSFileManeger removeItemAtPath:error:]
  4. Write downloaded data to a DB file with -[NSData writeToFile:atomically:].


Here is the full code

//replacing the db that I have emailed from app.

-(void)handleOpenURL:(NSURL *)url {

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *txtPath = [documentsDirectory stringByAppendingPathComponent:@"/DemoApp.sqlite"];


NSURL *newUrl = [[NSURL alloc] initWithString:
              [txtPath stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];


if ([fileManager fileExistsAtPath:txtPath] == NO) {
    [fileManager copyItemAtURL:url toURL:newUrl error:&error];
}
else if ([fileManager fileExistsAtPath:txtPath] == YES) {

    [fileManager removeItemAtPath:txtPath error:&error];
     [fileManager copyItemAtURL:url toURL:newUrl error:&error];
}

}

Hope this helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜