捆绑访问中的 sqlite 数据库路径?
我想将我的 sqlite 数据库放入一个自定义捆绑包中,并在多个项目中使用该捆绑包。
我从 Mac OS X FrameWork&Library 创建了一个新目标 ->捆绑包,我已经构建了目标,并且我已将我的 sqlite 数据库放入捆绑包中。 我已将捆绑包添加到我的 iOS 项目中并尝试从中读取数据库。
现在问题来了:我可以获取包并读取其标识符,但是当我尝试获取 db 的路径时,我得到 null 返回字符串。
读取数据库路径的代码:
NSBundle *myDbBundle = [NSBundle bundleWithPath:[[NSBundle mainBundle]pathForResource:@"myDbBundle" ofType:@"bundle"]];
NSLog(@"BundleIdentifier:%@",[myDbBundle bundleIdentifier]);
NSString *databasePath = [myDbBundle pathForResource:@"myDbName" ofType:@"sqlite"];
NSLog(@"DataBasePath:%@",databasePath);
欢迎有关此问题的任何建议、文档或教程。
更新: 看来我无法加载该捆绑包,这就是为什么当我尝试从中读取时失败的原因。
代码:
NSError *error;
NSLog(@"LoadBundle:%@",[myDbBundle loadAndReturnError:&error]?@"yes":@"no");
NSLog(@"Error:%@",[error localizedDescription]);
所以真正的问题是如何在放置 sqlite 数据库的地方创建一个包并且可以从 ios 应用程序读取?为了创建捆绑包,我在 Mac OS X 下使用了 xcode 模板,因此我认为我必须在构建设置中更改一些内容,但我不知道是什么。
I want to punt my sqlite db in to a custom bundle and use that bundle in more than one project.
I have created a new target from Mac OS X FrameWork&Library -> Bundle, I have build the target and the I have put my sqlite db in the bundle.
I have added the bundle to my iOS project and try to read the db from it.
Now comes the problem: I can get the bundle and read its identifier but when I try to get the path for db I get null return string.
The code to read db path:
NSBundle *myDbBundle = [NSBundle bundleWithPath:[[NSBundle mainBundle]pathForResource:@"myDbBundle" ofType:@"bundle"]];
NSLog(@"BundleIdentifier:%@",[myDbBundle bundleIdentifier]);
NSString *databasePath = [myDbBundle pathForResource:@"myDbName" ofType:@"sqlite"];
NSLog(@"DataBasePath:%@",databasePath);
Any sugestion, docs or tutorial about this issue are wellcome.
Update:
It seems that I can not load the bundle that is why when I try to read from it I fail.
Code:
NSError *error;
NSLog(@"LoadBundle:%@",[myDbBundle loadAndReturnError:&error]?@"yes":@"no");
NSLog(@"Error:%@",[error localizedDescription]);
So the real problme is how do I create a bundle in witch I place a sqlite db and witch can be read from ios app? To create the bundle I used the xcode template under Mac OS X so I think that I have to change something in the build settings, but I don't know what.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
应该足以在主包
编辑中获取 myDbName.sqlite 的路径:
好的,这就是我所做的,首先,新建文件 ->资源->设置Bundle,创建一个新的,将其命名为myDbBundle,然后右键单击新创建的bundle,在其中添加myDbName.sqlite,确保它不在en.lproj内。另外,请确保在创建新捆绑包时,勾选目标以将其包含到当前目标中,因为这不是 Settings.bundle,因此您希望包含它。然后运行你的代码,它工作正常。
这是我使用
NSLog(@"%@", dict); 的代码
打印出正确的值,这意味着它已成功加载should be good enough to get the path to your myDbName.sqlite inside your main bundle
EDIT:
Okay, here is what I did, first, New File -> Resources -> Settings Bundle, create a new one, name it myDbBundle, then right click on that newly created bundle, add myDbName.sqlite in it, make sure that it's not inside en.lproj. Also, make sure when you create a new bundle, you tick the targets to include it into your current target, since this is not Settings.bundle, you want to include it. Then run you code, it works fine.
and here is the code I use
the
NSLog(@"%@", dict);
is printing out correct value, which means it's successfully loaded我自己没有这样做,但这个博客似乎就是你想要做的iPhone iOS 2.0通用框架
它描述了创建一个框架,可以用来打包任何文件,然后根据需要简单地导入到其他项目中。
I haven't done this myself, but this blog seems to be what you are trying to do Universal framework iPhone iOS 2.0
It describes creating a framework that can be used to package up any files and then simply imported into other projects as needed.
Swift 3
let dbname = Bundle.main.path(forResource: "horscope-release", ofType: "sqlite")!
Swift 3
let dbname = Bundle.main.path(forResource: "horoscope-release", ofType: "sqlite")!