在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屋!
查看全文