1

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.

1
  • 1
    If you look in to the bundles themselves (with Finder etc) is the DB file there?
    – Peter M
    Jan 25, 2012 at 14:08

3 Answers 3

4
NSString *databasePath = [[NSBundle mainBundle] pathForResource:@"myDbName" ofType:@"sqlite"];

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.

enter image description here

and here is the code I use

NSBundle* bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"myDbBundle" ofType:@"bundle"]];
NSLog(@"%@", bundle);
NSString* test = [bundle pathForResource:@"myDbName" ofType:@"sqlite"];
NSLog(@"%@", test);
NSDictionary* dict = [NSDictionary dictionaryWithContentsOfFile:[bundle pathForResource:@"Root" ofType:@"plist"]];
NSLog(@"%@", dict);

the NSLog(@"%@", dict); is printing out correct value, which means it's successfully loaded

3
  • okay, my bad, I shouldn't really hastily answer the question. I can confirm that your code works perfectly, maybe it's that your myDbName.sqlite isn't added properly in your myDbBundle.bundle? or the myDbName.sqlite is actually contained in a folder inside your bundle?
    – X Slash
    Jan 25, 2012 at 13:52
  • First edit so I can remove the down vote. You created a new bundle and add to it an sqlite db. How you did it? What are the steps? I will edit my question because I see that myDbBundle,bundle could not be loaded. And I tested with myDbName.sqlite also in side and outside of a folder. Jan 25, 2012 at 14:08
  • The problem it was that I crated the bundle form Mac OS x as a new target. Your version with settings bundle is working fine. Thx for your answer. Jan 25, 2012 at 15:00
1

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.

1
  • Thx for answer, I ready know it. What I want is just a bundle not a framework. Jan 25, 2012 at 14:52
0

Swift 3

let dbname = Bundle.main.path(forResource: "horoscope-release", ofType: "sqlite")!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.