如何在iOS中启动应用程序时复制sqlite数据库? [英] How to copy sqlite database when application is launched in iOS?
本文介绍了如何在iOS中启动应用程序时复制sqlite数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我每次启动应用程序时,都要将我的sqlite数据库从数据库位置复制到我的iOS应用程序的最新更新。
I want to copy my sqlite database from the database location with latest updates to my iOS application every time I launch the application.
有什么办法吗?
推荐答案
添加以下方法到您的appdelegate
you can add following methods to your appdelegate
- (void) copyDatabaseIfNeeded {
//Using NSFileManager we can perform many file system operations.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dbPath = [self getDBPath];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if(!success) {
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"database.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (!success)
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
- (NSString *) getDBPath
{
//Search for standard documents using NSSearchPathForDirectoriesInDomains
//First Param = Searching the documents directory
//Second Param = Searching the Users directory and not the System
//Expand any tildes and identify home directories.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
//NSLog(@"dbpath : %@",documentsDir);
return [documentsDir stringByAppendingPathComponent:@"database.sqlite"];
}
并在启动方法中调用此方法
[self copyDatabaseIfNeeded];
希望这将有所帮助。
and call this method in your did finish with launching method
[self copyDatabaseIfNeeded];
hope this will help.
这篇关于如何在iOS中启动应用程序时复制sqlite数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文