4

I have a normal pre-populated database. I want to migrate it to core data for my app. What is the best way to achieve this. By the way I have done these things, copied the recipes.sqlite database from apple's sample.exported a table sql. Rename the column names according to the core data table columns(something like this - id "primary key" with Z_PK). filled that table with my values. But i am unable to understand the columns names Z_ENT and Z_OPT. Does some one knows how do i migrate my prepopulated sqlite3 database to core data easily.

Thanks

Jasper
  • 7,031
  • 3
  • 35
  • 43
Rahul Vyas
  • 28,260
  • 49
  • 182
  • 256

2 Answers2

6

Do not try and manually create a Core Data SQLite file. That is a road to failure; every time.

If you have a pre-existing SQLite file then use straight SQLite tools to access it and import it into Core Data using Core Data. Once you have the data in a Core Data stack, save out that file and then use the resulting SQLite file.

The internal structure of the Core Data SQLite file is designed to be opaque and should not be reverse engineered. Apple makes no guarantee that the file structure will stay the same. They have changed it several times already since Core Data was released.

Import

To do the import, it would be just like any other file:

  1. You stand up the core data stack.
  2. You walk through each table and each row in the non-Core Data database.
  3. Create a new Core Data object for each row.
  4. Insert the data from the old row into the new object.
Marcus S. Zarra
  • 46,571
  • 9
  • 101
  • 182
  • could you please tell me the steps? – Rahul Vyas Nov 09 '10 at 15:13
  • What about the Z_OPT column of core data? – Rahul Vyas Nov 12 '10 at 10:17
  • 1
    You shouldn't care. The internals of the core data file are an implementation detail. Only use Core Data to touch the SQLite file. – Marcus S. Zarra Nov 12 '10 at 17:52
  • I want to use prepopulated data. Also will i be able to use insert,update like we do in sqlite? – Rahul Vyas Nov 15 '10 at 14:10
  • 1
    you can do anything that Core Data can do which includes, updating, inserting and deleting data. I suggest you learn a bit more about Core Data so that you can better understand what it is and how it functions. – Marcus S. Zarra Nov 15 '10 at 19:59
  • @MarcusS.Zarra I agree with all of this counsel regarding the copying of data, that it's better to programmatically migrate the data rather than trying to go in through the "back door". But how about just the `.xdatamodeld`? I have a fairly rich database with a dozen entities and don't relish manually creating the data model. – Rob Nov 30 '12 at 19:14
  • I have never tried to create the model by hand. Off the top of my head you should be fine. Might take longer to reverse the format than create the model though :) – Marcus S. Zarra Nov 30 '12 at 22:32
1

Try the following tool which migrates the Standard SQLite to Core Data Compatible Sqlite

https://github.com/tapasya/Sqlite2CoreData

legend
  • 126
  • 1
  • 2