iphone any interface to deal with sqlite database created in the application Documents folder? - iphone

do we have any command line from where i can query the sqlite database, which is created by coding, and stored in the application's default Documents folder?

Turn on file sharing for the app, copy the database file to your Mac, and use the command line tools (sqlite3) that are there.
(Note to the previous editor: I appreciate editing of answers for accuracy, format improvements, and fixing typos...but, if you want to provide completely different information, I suggest providing your own answer instead of changing the meaning of another user's response.)

Related

Where is the SQL persistent store created by CoreData?

Can someone help me find where CoreData is storing the SQL file it creates in iOS applications?
So far I've tried "Show package contents" on the .xcdatamodel file and also the .xcodeproj file...No dice!
Is there an editor for looking at the contents of these SQL files?
Cheers.
-A
The url parameter of [NSPersistentStoreCoordinator addPersistentStoreWithType:configuration:url:options:error] specifies the location of the file. Find this call in your code and see where you've stored it. XCode templates will put it into the top of your application documents directory, but you can put it anywhere you like.
Note that Apple considers the contents of this database to be opaque and non-user-modifiable. Playing with the data in your store is likely to cause issues with Core Data.
It's typically saved into your application's documents directory, with the name of your data model.
So, for example, if my data model is called AppData.xcdatamodeld, it will be AppData.sqlite inside my app's document directory. You shouldn't really need to touch the .sqlite file though.
Hope that helps!
Core Data editor --> http://christian-kienle.de/CoreDataEditor/

How to dynamically create a sqlite3 database on the iPhone?

I would like to know if it is possible to create the file of a database by programming?
Actually I need to create a database if it does not exist.
I'll assume you have your own valid reasons for using sqlite3 directly rather than Core Data. There are certainly cases where it's appropriate.
The sqlite_3_open() function will create the database if it doesn't already exist. The sqlite3_open_v2() function will create the database if you pass SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE in the flags parameter. See the documentation for more details.
Of course on iPhone you'll need to make sure you're creating the database in a read-write directory such as the app's Documents directory, as opposed to the Resources directory, which is read-only.
In practice I've never tried building a database from the ground up on the iPhone. I always found it simpler to just include an empty DB file with the schema pre-built as an application resource, and then copy the file to the Documents directory the first time the app is run.
Are you sure that you need to create the database file directly? Maybe you should check out the Core Data Framework.
Thanks for your answers, I have never used Core Data so I will have a look on this.
For the moment I also copy a DB file from the Resource directory to the Documents but I would like to create a static library which can be used by many persons. So I would give a minimum of files to add to their project. That's the reason.

iPhone Simulating App Update at home before going out in the big bad world

this is a follow on from this question and the link given it seems that when an app is updated all of the files in the documents directory are copied into the updated apps documents directory and also anything in Library/Preferences. Whats the best way to simulate this for testing purposes?
Just copy the files in ApplicationSupport/iPhone Simulator etc? or has anyone developped any funky techniques for testing this.
I have created .sqlite file in my application's documents folder and created some dummy entries in my database table.
Now just change you code build new application without changing any of the certificate and profile information load the new application.
Now check for the old database table records. It will be definitely preserve under your Document directory in .sqlite file.
This is what i followed when i want to check it for same thing.
If anyone know better way then please feel free to suggest new things.
Jim.

How to programmatically fill a database

I currently have an iPhone app that reads data from an external XML file at start-up, and then writes this data to the database (it only reads/writes data that the user's app has not seen before, though)
My concern is that there is going to be a back catalogue of data of several years, and that the first time the user runs the app it will have to read this in and be atrociously slow.
Our proposed solution is to include this data "pre-built" into the applications database, so that it doesn't have to load in the archival data on first-load - that is already in the app when they purchase it.
My question is whether there is a way to automatically populate this data with data from, say, an XML file or something. The database is in SQLite. I would populate it by hand, but obviously this will take a very long time, so I was just wondering if anybody had a more...programmatic solution...
I'm going to flesh out Jason's answer, I've marked my post as a community wiki so I shouldn't get any points for this.
He's not talking about a dummy app - write the app as you normally would, but check to see if the database exists in your main bundle before you call the code that populates the plist. You run that in the simulator, pull out the generated sqllite database, and add it to your project - if you only need to read from it, you can read it from the main bundle directory. If you need to do further writes then copy it into the writable documents area, and use it from there. So basically for the main user, the code to populate the DB would never be called...
The only downside is you also end up including the plist files you are reading from, even though you only need the database. You could make a different build target that was a copy of the main one with the only difference being that it held the plist files, while the main target you built for the app store did not.
Not to take Jason's answering thunder, I can't comment yet so it has to be here.
The nice thing is that you can access the filesystem of the simulator right on your Mac. I am away from mine at the moment or I could tell you exactly where to look, but I just find it by putting the name of the db file into searchlight and just running with that.
You also do not need to wright any code to populate the db since you can use the command line tool to do the initial setup if that is more convenient.
You will need to copy it over though since resources are stored in the read only signed portion of the app bundle.
I had the same problem of you using sqlite, on massive insert it's really slow. So the best way it's provide directly a filled sqlite database.
You have another way, instead of INSERT INTO, to populate a sqlite db. You can produce a csv file for each table and load into the tables using your computer and the sqlite shell:
Just 2 simple commands:
.separator SEPARATOR
.import FILE TABLE
Example:
adslol:~ user$ sqlite3
SQLite version 3.6.12
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .separator ;
sqlite> .import myData.csv nameOfMyTable
sqlite> .quit
I hope it's what you was looking for :)
If you need a good client for sqlite3 try SQLite Manager, it's a Firefox add-ons.

Prefilling Core Data for a read-only application

i am working on an application that displays read-only data i am shipping.
it is more of a book.
It is easy with SQLite but i am not satisfied with the performance and trying to use Core Data.
The issue is with pre-filling Core Data is that it is a hard process.
My question is:
Is it possible to build an assistant iphone application (for me to use) which uses the same data model for pre-filling.
and then take the populated .xcdatamodel file and use it in my original application?
I hope this makes sense :)
Adham
I believe what you're asking is whether you can create a CoreData database upfront and copy it to the iPhone. Is that correct?
This article will help. Here's a quote:
I thus suggest the following five-step process:
Create your data in a comma-separated file, typically placing each row of data (an entity) in a row of the file and separating different columns (its attributes) by commas.
Write a standalone program and copy in your .xcdatamodel file from your main project.
Write code in your new program that parses your comma-separated file and inserts the information into a Core Data persistent store that should be identical to the persistent store in your main project.
Run the program in the Simulator
Copy your data from the Simulator's documents directory into your actual project's bundle.
It's possible, I've done it. I made a desktop application to read from a CSV file using the code here:
http://www.mac-developer-network.com/columns/coredata/may2009/
I just had to alter the way the CSV part worked, and change the model.
I copied and pasted my model from the model builder into the iPhone model. (Clicked on the "grid" area, selected all, copied)
Then I took the sqlite database the desktop app produced (found it in Application Support, in the folder for this application) and put it into the resources folder
I made some code to copy the sqlite into the documents folder on the iPhone (if it wasn't already there) at startup, in the applicationDidLaunch method. It's possible that having it in the resources folder is no good. Even though you're using the database as read only, Core Data may want to write something to it. Not sure about this though..
I used the sqlite file in the documents folder in my Core Data set up.
The desktop and iPhone Core Data sqlite file seem to be exactly the same format. You can transfer one sqlite file to another application (iPhone to iPhone too) as long as they have the same data model. In another application, I used NSXMLParser to create the Core Data sqlite file, then transferred it to another app, both on iPhone using the Simulator.
Yeah, your data source can be whatever you want it to be. The other suggestions are good ones. Create a managed object model (.xcdatamodel) identical to what you want to use in your app. Read in the data from your file, create a new instance of your managed object and populate it from the file. Then save, and dive into the bundle in the iPhone Simulator and copy it over. This has the added bonus of being in exactly the format you need, with all the helpful metadata. Copy your object model and your managed object classes and you're good to go.
Note, though, if you really intend for it to be read-only, and you're using it at install, it will be installed in your finished app's bundle (under Applications/{SIGNATURE}/Myapp.app). If you intend to edit this database or allow a user to save to it, it's a better idea to copy it to the Applications/{SIGNATURE}/Documents directory where your user database lives.