Access pre-existing database(read only) from iOS bundle - ios

I have pre-created database stored in a folder inside my bundle. Can I access it directly from that folder without copying to Document folder?. I don't want to write anything to Db. Only want to read some data. I am using FMDB framework also.

You can get the path to your database using:
NSString *path = [[NSBundle mainBundle] pathForResource:#"someDB" ofType:#"sqlite"];
Change the parameters to match your database. And then provide that path to your framework so you can access it.

Related

iOS: Is this the proper way to setup a database location under the application support folder?

I have a sql file that needs to be stored in my App. A user on this site recommend me to store it underneath the Application Support folder. This is the path directory that I am using with my fmDatabase library.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *applicationSupportDirectory = [paths firstObject];
self.dataBasePath = [applicationSupportDirectory stringByAppendingString:#"Email.sqlite"];
self.fmDataBase = [FMDatabase databaseWithPath:self.dataBasePath];
Everything seems to be working fine. However, I am a little nervous that I may be doing something wrong. I am using the Application Support directory because I don't want the OS or an update to interfere with the Database. I would use the document directory ,but my App supports file share so the user can mess it up.
(application_home)/Library path is a place where you can create custom subdirectories for files you want backed up but not exposed to the user.
create a directory such as (application_home)/Library/Database and save files there.
for mac os x with sqlite The convention is to store it in the Application Support folder .
i.e. in ~/Library/Application Support/YourAppNam*/database.db
I think the data will be saved on the Application Support Folder in Mac platform, for iOS, the default is xxx/Library.
I don't see any dangerous element on this, because you can save to different folder as long as you can load it from right path.

is their a way to bundle sqlite database file with os x application? for both to be one file

I tried sqlite FMDB but it must store database into separate file I want solution that the database file is the same executable file ie they are bundled together into one app please help
Here's how to do it: create database with a file handle for the file system produced in the traditional way (NSSearchPathForDirectoriesInDomains and stringByAppendingPathComponent: #"mydb.sql").
Sounds like you've got that done. Drag the resulting file into your app. Once in your app bundle, the way to get at the file is like this (using the filename I made up above):
NSString *fileName = [[NSBundle mainBundle] pathForResource:#"mydb" ofType:#"sql"];
// or whatever you called the file in your bundle, use ofType: as the filename's extension
That should suffice as input to the FMDB package.

coredata - deliver / setup default data

I use coreData in my iOS App. It's possible, that the user Add, Delete Data into the Database.
I have to deliver default data ( some different data-sets ).
At the moment, I'm creating the database by first Application launch. I read data from a csv file an create the database with it.
The csv is in the Application sandbox; the coreData (managedDocument) is in ApplicationDocument (creation on runtime...).
It works perfect for me - but I ask me, will Apple allow that, if I push the App to the AppStore?
There is nothing wrong with this approach and it can't be a reason for rejection. There is also another way to do it. You can create the database the way you do it now, copy the .sqlite file and provide it as your default database. Then copy it on app first run. The following code will do it:
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent: #"YourDBName.sqlite"];
if (![fileManager fileExistsAtPath:storeURL.path]) {
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:#"YourDBName" ofType:#"sqlite"];
if (defaultStorePath) {
[fileManager copyItemAtPath:defaultStorePath toPath:storeURL.path error:NULL];
}
}
With this approach you will not need to include your csv file in your bundle.
Yes, apple does allow shipping a database populated by default.
the standard way to do it is to ship a default database in your bundle, then at launch time check if there is a database in your application documents directory, and if it does not exist, then copy the database from your bundle to the documents directory.

SQLite file is not found when i run on the device

I have an SQLite database file embedded into my project. On the simulator, querying the database works fine, but not on the device. My database file path when i run on the device is this (as shown on the console with an NSLog) :
The path for the database file is : /var/mobile/Applications/5914F328-148F-52E6-1AC9-38D7FF141F9B/MyApplication.app/db.sqlite
My relevant code which looks for the DB is:
NSFileManager *fileMgr=[NSFileManager defaultManager];
NSString *dbPath=[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"db.sqlite"];
NSLog(#"The path for the database file is : %#",dbPath);//Display the whole path
I know that the path on the simulator and on the device are completely different. So how should i change my code above to find my database file relatively?
I solved my issue, for whom that struggle on the same problem, it needs me just to include my SQLite file db.sqlite into my Bundle resources. CLick your project target-->Build Phases--->Copy Bundle Resources and then hit the + button and add your SQLite file or just drag your SQLite file from project Group and files and drop it there. Hope this help :)

File operation not working on device

What could make a file operation that is working well on the simulator, to not be working on an iOS device?
When using [NSBundle mainBundle], and the file is found by FileManager, what could be the different reasons for adjacent file operations to have different outcome?
I am noticing this sometimes, and just want to get an idea of what to think about when this happens.
Seems like you are trying to read a file in your application bundle. You may get its path by code:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"fileName" ofType:#"ext"];
//Then you can use NSFileManager to read/copy it
All files in application bundle are readonly. You may get more information from here:
http://developer.apple.com/library/ios/#documentation/CoreFoundation/Conceptual/CFBundles/AccessingaBundlesContents/AccessingaBundlesContents.html#//apple_ref/doc/uid/10000123i-CH104-SW8
As #Aadhira said in the comment above, the simulator stores its files inside a bunch of folders on your mac, not in some sort of simulated main bundle/docs directory sandbox.
In order to get a static file from your main bundle you must create a path starting from [NSBundle mainBundle] and add path components onto it.

Resources