我正在使用此代码加载本地sqlite数据库。
- (id)init
{
self = [super init];
if (self) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"db" ofType:@"sqlite3"];
_db = [[MDDatabase alloc] initWithPath:path];
_HTMLRenderer = [[MDHTMLRenderer alloc] init];
}
return self;
}
我想将数据库放在网上,让应用程序下载数据库。我将代码更改为:
NSData *dbFile = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.someurl.com/DatabaseName.sqlite"]];
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"Database.sqlite"];
[dbFile writeToFile:filePath atomically:YES];
_db = [[MDDatabase alloc] initWithPath:filePath];
_HTMLRenderer = [[MDHTMLRenderer alloc] init];
Editted: 我改变了我的代码,但它已经崩溃了。
- (id)init
{
self = [super init];
if (self) {
[self performSelectorOnMainThread:@selector(downalod) withObject:nil waitUntilDone:YES];
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"Database.sqlite"];
_db = [[MDDatabase alloc] initWithPath:filePath];
_HTMLRenderer = [[MDHTMLRenderer alloc] init];
}
return self;
}
-(void)download
{
NSData *dbFile = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.someurl.com/DatabaseName.sqlite"]];
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"Database.sqlite"];
[dbFile writeToFile:filePath atomically:YES];
}
答案 0 :(得分:0)
在.m
中声明此宏#define DocumentsDirectory [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
现在打电话,您将在本地获得该文件。
请致电:
[self performSelectorOnMainThread:@selector(downalod) withObject:nil waitUntilDone:NO];
_db = [[MDDatabase alloc] initWithPath:filePath];
_HTMLRenderer = [[MDHTMLRenderer alloc] init];
-(void)download
{
NSString *stringURL =[NSString stringWithFormat: @"url"];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",[stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSString *filePath = [DocumentsDirectory stringByAppendingPathComponent:[url lastPathComponent]];
NSLog(@"success--222,%@", filePath);
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
NSLog(@"success--222,%@", filePath);
if (error)
{
NSLog(@"success--not---Error,%@", [error localizedDescription]);
}
else
{
NSLog(@"success--yes... %@", [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]);
[data writeToFile:filePath atomically:YES];
}
}];
}
答案 1 :(得分:0)
经过长时间的搜索,终于我自己解决了这个问题:
- (id)init
{
self = [super init];
if (self) {
NSData *fetchedData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.someurl.com/db.sqlite3"]];
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"db.sqlite3"];
[fetchedData writeToFile:filePath atomically:YES];
_db = [[MDDatabase alloc] initWithPath:filePath];
_HTMLRenderer = [[MDHTMLRenderer alloc] init];
}
return self;
}