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
- Download your file with +[NSData dataWithContentsOfURL:].
- Close sqlite DB, if opened.
- 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:]
- 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
精彩评论