How to see core data tables and how to delete the data from a real device - iphone

Two questions about Core Data:
1. Is it possible to see the actual data(the db tables) that is stored when using core data?
2. When using the iPhone simulator i can delete the core data db by resetting the simulator. But how can i delete the core data db when using a real device?

Your Mac comes with a command line tool sqlite3. You can use it to do queries against the core data database. Not sure if that violates any rules against reverse engineering that might exist in any license agreements.
You can delete the db for convenience during development by deleting your app from the device and reinstalling it. If you don't want to do it by deleting the app, you will have to write some code in your app to delete it I think.

You can directly view an sqlite store using any sqlite tool including the command line sqlite3 tool. However, (1) Core Data uses a custom undocumented schema so looking at the store doesn't tell you much and (2) the structure of the sqlite store file itself has little relationship to how the managed objects behave in memory. Core Data isn't an sql wrapper. It just takes the managed objects apart to store them in SQL. SQL is just an option and Core Data works fine without it.
To "delete the core data db" you simply delete the persistent store file. Look where you initialize the persistentStoreCoordinator (in the Xcode templates, it's in the app delegate.) The URL for the persistent store/s will be there. Just close down the Core Data stack and then delete the store file with NSFileManager. Then restart the Core Data stack.

Related

Migration options for preloaded sqlite database in iphone app

My app uses a preloaded SQLLite database with coredata bindings. I create the preloaded database file with a secondary app that has the core data bindings/model etc. I put the preloaded db into my app's resources folder and copy that to the writable documents folder when the app is first run.
My question is about migration. Since my app released, my users have been creating data and in my next release I want to change the data model and migrate some of the data to the new model. I'm just not sure the best way to do this. I've read about core data migration methods but they seem to require my app to be a core data project, which it isn't (though my secondary database-creating app is). Will I have to do this migration manually by issuing direct SQL statements against the db? And what about the core data bindings, will I have to manually update them?
I'm really just looking for pointers in the right/best direction. Any suggestions? Thanks in advance!
SQLLite doesn't really have any data migration tools like Core Data. Unless you count "ALTER TABLE". If the migration is very complex, or you think you will have more complex migrations in the future you could make a Core Data model that more or less lines up with the old SQLite model and import to it, and then migrate to a newer Core Data model, and then stick with it.
This is likely slower then ALTER TABLE though, or at the very least "not faster".

moving data to core data

i have a .sqlite file with all the data i want to be used with my iPhone app .
but i feel i should be using Core data for what i want done.
is it possible to some how move all the data that is held within my .sqlite file to core data's .sqlite file ?
i have created only 2 fields in my .sqlite file and 2 attributes in the core data file, but i don't think i can replace the coreData's sqlite file as core data added additional fields to it.
what is the best way to handle this .
thanks
Since you generated the database yourself, the best way to move the data into a Core Data store is to use Core Data itself to re-generate the data. You can do this either through a throw-away app or within your app itself.
The fact that Core Data on the iPhone uses SQLite as a backing store is an implementation detail. Trying to recreate the core data store yourself may to cause very obscure bugs, you'll be much safer by allowing Core Data to generate the database.
#ExtremeCoder's answer is an option, but even writing an app to read from SQLite and store into Core Data would be a better choice than what that blog suggests.
Check this out: http://ablogontech.wordpress.com/2009/07/13/using-a-pre-populated-sqlite-database-with-core-data-on-iphone-os-3-0/

Creating .sqlite file from Core Data store?

I've seen tutorials in books and on websites that offer .sqlite files for download. The sqlite files are used for Core Data.
How do I get a .sqlite file FROM an app or core data store on TO my desktop?
If you are going to create a pre-populated sqlite file to be used with Core Data then you must create it with Core Data. It is trivial to create a basic desktop application for data entry and use that to create the file and then embed it in your iOS device.
Do not attempt to duplicate the data structure or linkage within the file by hand. You might get it to work but you will spend way more time trying to get it to work and it is going to eventually fail. The internal structure of a Core Data sqlite file should be considered like a private API. The structure is not public and can change without notice.
If you are specifically trying to create a Core Data store, you use this method:
NSPersistentStoreCoordinator
addPersistentStoreWithType:(NSString*)storeType
configuration:(NSString*)configuration
URL:(NSString*)storeURL
options:(NSDictionary*)options
error:(NSError**)error
You must have already associated a NSManagedObjectModel with your persistent store coordinator object. If the store at storeURL does not exist, it will be created; otherwise it will be opened.
These might help: http://www.sqlite.org/cvstrac/wiki?p=ManagementTools
I like to use this Firefox plugin:
https://addons.mozilla.org/en-US/firefox/addon/5817/
You can create a new .sqlite file, change existing databases, and browse through your data.
There's a command line program download-able from sqlite.org (in the standard download) that can be used to create a blank database with a schema. Usually the database file is compatible across operating systems and devices.

How can I create a sample SQLite DB for my iPhone app?

I'm diving in to iPhone development and I'm building an iPhone app that uses the Core Data framework and my first task will be to get the model setup with a view that will display it. Thus far, I have the model defined and my Managed Object Files created, but I don't have a database with any sample data.
What's a quick way to create a DB that conforms to my schema?
Are there any tools that can generate a sample DB using my schemas? Or do I have to create this sample data by hand?
Once the DB is created, are there any good tools I can use to directly manipulate the data in DB for testing purposes?
Thanks in advance for your help! I'm going to continue researching this question right now.
This is very close to the question "Provide Base Data for Core Data Application?" Additionally, my answer to this question describes how you can quickly build a Mac application that lets you create or edit a Core Data database that is compatible with your iPhone application's data model.
Beyond that, you can use the application Core Data Editor to do what its name describes.
I assume you've already created a working app that uses sqlite as persistent storage for your data model.
Have a look into the AppDelegate.m file to search for the sqlite database name and location, then run your app in the iPhone Simulator.
Use Spotlight to search for the SQLite database created by the app in the simulator, usually this is /Users/<Username>/Library/Application Support/iPhone Simulator/User/Application/<Application GUID>/Documents/<database name.sqlite>
Now you only have to copy that file to a working folder, open it using sqlite3 (www.sqlite.org), then type .schema to retrieve the database schema.
Now populate it, either by hand or using a python/ruby/whatever script!
Unfortunately, i'm not aware of any tool that will populate a db by simply feeding them the schema.
For directly manipulating the data, sqlite3 provides you with a command line utility that's really handy for that purpose.
When you're finished, add the file with sample data to your App project.

How to add database layer in core data application

I am fairly new to core data technology and i searched a lot on how to add the database to a core data application.so can anybody guide me on how to integrate the database layer? i have seen the iphone tutorial on core data (i.e books example) but i am not able to understand how to .sqlite file has been included in that application
The SQLLite file is automatically generated at startup by core data, by the persistent store manager.
If you have a project generated to start with Core Data, look in the App Delegate at the persistentStoreCoordinator method - that's where it manages the file created and sets the path where it will exist. If there's an existing one it will make use of it, though you'd have to copy over a pre-loaded one into the writable path it sets up for the database.
Core Data is NOT a database, it is a persistent object store. You have no control over how the data is stored in the file. (So trying to get it to use your own designed database is a bad idea.) You only get to chose wether it uses XML, binary or sqlite as its backstore.
To see how your app gets the data from the file, look in the app delegate. (That is where most sample code put it.) You'll find some methods for the managed object context, and the persistent store coordinator. The latter will create the file if needed. Besides the save call during quit, or other relevant times, you don't have to do much with it. (You can do some stuff, but I can't recommend that when you're new to Core Data.)