moving data to core data - iphone

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/

Related

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

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.

Any FREE SQLite Manager for MAC OS X?

I am learning Core Data for iPhone application. I defined .xcdatamodel. But I have the following questions:
Is it possible to make .sqlite file
from the .xcdatamodel file ?
If not, what is the correct
procedure to prepare .sqlite ?
If it is necessary to use external
tool, is there any FREE tool to make
.sqlite ?
Thanks.
Your questions 1 & 2 have already been answered but here's a comprehensive overview for Q3:
http://www.barefeetware.com/sqlite/compare/?ch
I personally use a (free) Firefox add-on called SQLite Manager - you can download it from here https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/
Is it possible to make .sqlite file
from the .xcdatamodel file ?
Well, when you create a Core Data stack and set your store to an SQLite store, the persistent store coordinator will create a .sqlite file configured for the model attached to the store.
If not, what is the correct procedure
to prepare .sqlite ?
Before iOS 3.0, you couldn't use Core Data so there were several libraries out there for using SQLite. However, I don't think any of them have been updated because there is not much point when using Core Data.
If it is necessary to use external
tool, is there any FREE tool to make
.sqlite ?
SQLite comes standard as part of MacOS X so you can use the command line or scripting languages like Ruby, Python, Perl (also standard) to create any SQLite database you want.
But honestly, I wouldn't bother. Unless your app's data is very simple and largely static, you will end up reinventing the wheel and effectively reproducing most of Core Data just to interface SQLite with the rest of the app.
Core Data's SQLite structure really isn't designed to be handled by anything other than Core Data. Even if you have your persistent store use the SQLite data format, if you open it in a generic SQLite tool you're going to get a cryptic mash of nothing-you-can-reliably-mess-with.
I'm guessing your goal is to have pre-populated data in your database? The right way to do that is to write some “importer” code that reads in whatever existing data you have and creates objects in the persistent store corresponding to those. Keep a copy of that persistent store—maybe in your app bundle, to be copied out to a temporary directory for writing—and you've got yourself a starting data set.

iPhone: Storing video and audio in Core Data?

I need to add video and audio recording functionality to an existing app that uses Core Data. I've been thinking that I should save them to the file system and reference them using Core Data but I wondered if I can put them into Core Data as Transformable attributes and ignore the file system altogether?
My gut feeling is that this is a Really Bad Idea™. I do like the ease and value-add of using Core Data however.. I've tried searching for 'iphone core data video' but that just returns heaps of core data tutorials.
Don't put binary data in Core Data. Store them on the file system and store a reference to the file system location in Core Data.
If you store them in Core Data you risk blowing the cache and causing terrible performance, not to mention probably blowing out memory as you try to fault in a large amount of data.
Coredata has a 'allow external storage' option when saving binary data with coredata which will automatically store files larger than 1mb to the disk.
However, this question is very similar to these questions:
https://softwareengineering.stackexchange.com/questions/150669/is-it-a-bad-practice-to-store-large-files-10-mb-in-a-database
and
Storing large (e.g. image/movie) files in Core Data on the iPhone
In contrast to the selected answer, Core Data should be able to maintain good performance as long as there isn't a huge amount of videos or photos.

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