每当我启动应用程序时,我想将数据库中的sqlite数据库从最新更新复制到我的iOS应用程序.
有什么办法吗?
解决方法
你可以添加以下方法给你的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];希望这将有所帮助.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。