SQLite file is not found when i run on the device - ios

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 :)

Related

NSBundle doesn't load dynamic resources

I have a problem reading a Strings file created dynamically inside my app's document directory.
Basically, I create and read my file using:
[[NSFileManager defaultManager] createFileAtPath:filePath contents:fileData attributes:nil];
NSLog(#"%#", [targetBundle localizedStringForKey:#"MY_STRING" value:#"#" table:#"test"]);
The file is created in the document directory, and the targetBundle also points to this directory.
The above code works very well when the strings file already exists in the document directory when the app runs.
So, if I delete the Strings file from the document directory, the NSLog displays: #.
When I run again the application (the file already exists), NSLog displays MY_LABEL's value.
I guess that the bundle loads its resources once at the launch, but how can I be sure to read my new file even if it has just been created?
Thank you for your help,
Julian
I didn't find how to reload a bundle but I fixed this problem by creating an empty file with the same name BEFORE the creation of the bundle.
With that, the bundle "knows" the file and if the file dynamically changes, the content is up-to-date (because the file is read at runtime).

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.

Can you release an iOS app with a pre-populated sql db

I'm trying to load an sql database full of assets for my app to use upon release.
As I understand it, you can save the database to the applicationDocumentsDirectory:
NSURL *storeURL = [[self applicationDocumentsDirectory]
URLByAppendingPathComponent:DATABASEFILENAME];
But that gets saved locally on the simulator or the test device so I'd need to keep the source assets and release them with my app to generate that sql database.
I could release the app with the assets, populate the db on first run and delete the assets. Is there anyway to just include my pre-populated DB?
Yes. Simply copy the generated DB from your simulator Documents/ folder (file will have .sqlite extension) and put it in your project - making sure to add it to the package...
Yes , certainly there's a good way. Please see my answer in the link below. I have written with explanation and sample code. Hope that will help you.
Reading an already made sqlite file on ios

How to make Xcode Iphone Simulator save SQLite database to local file?

I am building my first iPhone app with CSS, HTML, Javascript and PhoneGap. I want to use SQLite as the database, so i am using the following tutorial as inspiration:
http://coenraets.org/blog/2011/10/sample-app-using-the-phonegap-database-api/
The following code determines weather to use data from the database, or to populate the database with data first:
function onDeviceReady() {
db = window.openDatabase("EmployeeDirectoryDB", "1.0", "PhoneGap Demo", 200000);
if (dbCreated)
db.transaction(getEmployees, transaction_error);
else
db.transaction(populateDB, transaction_error, populateDB_success);
}
After debugging, i found out that every time i run the app, it populates the database, so
if (dbCreated)
never executes.
I want to have a look at the SQLlite file, so i am using SQLite Database Browser to locate it, without luck. From the following question:
How to view the data in sqlite file running in iphone application?
I can see that the file should be located somewhere in this folder:
user/Library/Application Support/iPhone Simulator/
But the iPhone Simulator folder does not exist.
How do i make the simulator save the database to a local file?
Each time you run your application on simulator, it creates a folder inside iphone simulator folder .Make sure your Library folder is visible and you are using the correct path.
To make library folder visible you can run the following command on terminal:
chflags nohidden ~/Library
Here`s the path for my iphone simulator folder to use as a reference:
/Users/alexandreoliveira/Library/Application Support/iPhone Simulator
Another thing to notice, usually the sqlite database get saved inside documents folders:
Users/alexandreoliveira/Library/Application Support/iPhone
Simulator/5.1/Applications/some-weird-numbers/Documents/

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