views:

545

answers:

3

I have a sqlite DB created by my CoreData model automatically, but my app will not give the user the ability to write data into it, instead I wanted to pre-populate it with all the data the program needs. My problem is that the sqlite DB created by CoreData has unknow tables and fields that where not present in my model. Is there anyway I can populate the DB created automatically with sqlite and only then use CoreData to read it?

+3  A: 

The sqlite store created by Core Data should not be edited directly. Use Core Data to populate it.

Joshua Nozzi
So I would have to write an app just for populating the DB?
Reonarudo
Yes, you would need to create a small application to generate the Core Data sqlite file.
Marcus S. Zarra
Well, you *could* do it from within your main app, if you detect the database hasn't been populated. Otherwise, a quick non-document, core-data-based Cocoa desktop app with the same data model should work just fine. Use the drag-from-model-to-IB shortcut to create a basic interface, then you have a full app you can always use for your own development and editing, should your data need to be changed. Should take just a few minutes if you already have your model defined. Then just copy the database file it creates in ~/Library/AppName
Joshua Nozzi
I'm sorry, but do you mean by "Use the drag-from-model-to-IB shortcut"? I am not aware of such shortcut. Just drag it from one place to another?
Reonarudo
It's in the documentation and a very handy tip: http://bit.ly/bISZCg
Joshua Nozzi
I did as you sugested and strangely, for the same DB model, Core Data saved all data into a xml instead of the sqlite file. Is there a workaround for this?
Reonarudo
Change the store type to NSSQLiteStoreType?
Joshua Nozzi
Thank you, that solved it, I'm really unfamiliar with core data.
Reonarudo
+1  A: 

How did you create the original sqlite db you're trying to use? If you created it with Core Data then you should be able to copy it into your app as long as the model is the same.

You should prepopulate it using core data. This might be useful www.pragprog.com

Griffo
+1  A: 

The best way is to ship a prepopulated sqllite database. If you have to update the database or want to perform the initial load, I can highly recommend this entry: http://cocoawithlove.com/2009/11/writing-parser-using-nsscanner-csv.html

Shortseller