所以我有一个数据库的应用程序。这个数据库位于主包中。但是,我需要删除这个数据库,并从互联网上获得新的数据库。据我了解,我不能删除捆绑任何东西,并添加新的东西,所以我需要处理设备文档目录。但是我不知道如何使第一个数据库在文件目录下而不从网上下载。谁能帮忙?设备目录
Q
设备目录
0
A
回答
0
文档目录的路径,并需要将您的数据库文件放在那里。您需要从应用程序文档目录中复制的数据库中对应用程序执行所有读取/写入操作。
我使用这些方法在文件目录下创建我的数据库的副本: 你只需要调用这个方法:
+(BOOL)copyDBFileToDocumentsDirectory:(NSString*)dbFilename overwrite:(BOOL)yesNO
,只是需要通过文件名以扩展。
+(NSString*)databaseABSPath:(NSString*)databaseName existsInDocumentsDirectory:(BOOL)existenceStatus
{
BOOL success = NO;
NSString *databasePath = nil;
if(existenceStatus)//documents Directory
{
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
}
else //from bundle
{
NSArray *resourceSeparatedByExtension = [databaseName componentsSeparatedByString:@"."];
if([resourceSeparatedByExtension count]<2)
{
NSLog(@"Problem with database name or extension");
return nil;
}
databasePath = [[NSBundle mainBundle] pathForResource:[resourceSeparatedByExtension objectAtIndex:0] ofType:[resourceSeparatedByExtension objectAtIndex:1]];
}
NSFileManager *fileManager = [NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:databasePath];
if(!success)
{
NSLog(@"Database file does not exist at path");
return nil;
}
else
{
return databasePath;
}
}
+(BOOL)copyDBFileToDocumentsDirectory:(NSString*)dbFilename overwrite:(BOOL)yesNO
{
NSString *filePath = [mySQLiteDb databaseABSPath:dbFilename existsInDocumentsDirectory:NO];
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *databasePath = [documentsDir stringByAppendingPathComponent:dbFilename];
if(filePath)
{
NSError *error = nil;
if(!yesNO && [[NSFileManager defaultManager] fileExistsAtPath:databasePath])
{
NSLog(@"File already exist at path - instruction is not to overwrite");
return YES;
}
else
{
[[NSFileManager defaultManager] removeItemAtPath:databasePath error:&error];
}
error = nil;
[[NSFileManager defaultManager] copyItemAtPath:filePath toPath:databasePath error:&error];
if(!error)
return YES;
NSLog(@"Error copying file :\n\n%@\n\nuserinfo:%@\n\nFailure reason: %@\n\n",[error localizedDescription],[[error userInfo] description],[error localizedFailureReason]);
}
return NO;
}
0
您可以通过请求捆绑软件中的资源路径来到达捆绑包。例如
NSString *path =[[NSBundle mainBundle] pathForResource:[self.imagesDict objectForKey:key] ofType:@"png"];
0
你可以很容易的路径与此
NSString* pathToDocument = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
记录这将返回的路径为“文档”文件夹,您可以访问读到这里,你需要得到&写入数据
相关问题
- 1. Linux设备驱动程序字符设备“子目录”
- 2. 从Android设备挂载目录
- 3. 设置记录设备alsa
- 4. 记录设备ID
- 5. 移动Zpanel默认备份目录或Opendir备份目录
- 6. shell脚本备份源目录备份目录
- 7. 从并行项目目录添加外部设备
- 8. 目标设备从选定的目标设备上消失WHLK
- 9. pclzip备份两个目录
- 10. 镜像rsnapshot备份目录
- 11. 文档目录和备份
- 12. 备份大文件目录
- 13. 更改PHP备份目录
- 14. Python脚本备份目录
- 15. InstallShield回滚备份目录?
- 16. 目录设计
- 17. 查看设备登录
- 18. 设备登录的麻烦
- 19. 设计模式备忘录
- 20. Android:登录Android设备
- 21. 崩溃登录设备
- 22. 使用设备登录
- 23. 更改目标设备2008
- 24. iPhone目标某些设备
- 25. 项目在苹果设备
- 26. 的多个目录PHP_CodeBrowser目录设置
- 27. 设置项目根目录?
- 28. Android设备中的摄像机相册目录
- 29. ios设备文档目录中的多个音乐文件
- 30. 开始使用谷歌目录API和设备管理
所以这种方法从数据包复制数据库? – Noobass 2012-01-28 08:38:42