前端之家收集整理的这篇文章主要介绍了
在iOS中启动应用程序时如何复制sqlite数据库?,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
每当我启动应用程序时,我想将
数据库中的
sqlite
数据库从最新更新复制到我的iOS应用程序.
有什么办法吗?
@H_
502_7@
你可以
添加以下
方法给你的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];希望这将有所帮助.