我使用這段代碼加載本地sqlite數據庫。從URL下載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];
}
有用於獲取正確的文檔文件夾中引用的例子不勝枚舉。請在'NSDocumentDirectory'上搜索。 – rmaddy