1

Possible Duplicate:
Migrate normal sqlite3 database to core data?

Is it possible to load an existing sqlite db into core data? I'm facing troubles, Actually in the core data sample I found that it contains a db which is with z prefix to table name, and other attributes. What's the significance of that z? Also I saw in this blog http://ablogontech.wordpress.com/2009/07/13/using-a-pre-populated-sqlite-database-with-core-data-on-iphone-os-3-0/ to create a db using core data and load it which is a work around but not the proper method to load data to core data. I need to do lot of updates and queries on the table, please suggest whether to use sqlite directly or core data? If its core data, please suggest the best way to import existing sqlite file to core data

Community
  • 1
  • 1
Raj iOS
  • 1,047
  • 1
  • 9
  • 20

3 Answers3

7

The fact that Core Data uses SQLite is supposed to be an implementation detail. In fact, the Core Data documentation specifically say that you should not, in any way, access the database itself outside Core Data.

Likewise, you are not supposed to force your own database underneath Core Data.

If you want to import an existing database into Core Data, you need to write some code that will iterate through each entry in your existing database, and create corresponding objects to be inserted into Core Data.

FWIW, the Z_ stuff is Core Data decorations on their database.

Do not mess with the database. Now, it turns out that you can do a few things with the database, but you really should't -- unless you are an expert -- and then you still shouldn't, but probably will.

Jody Hagins
  • 27,943
  • 6
  • 58
  • 87
3

You mat want to check out Ray Wenderlich's Tutorial on Preloading existing data into Core Data : Check it out here It's a pretty complete tutorial and it would help you clear out some concepts regarding the use of Sqlite and Core Data together. It's a three part tutorial. Give it a shot.

  • 2
    Isn't there a simpler way? Is it required to create the mac command line tool with two projects(As it's series of tutorial in which the link you have given is second part) to import an sql? Looks using Sql directly to be simpler!! – Raj iOS Sep 07 '12 at 10:37
  • You already created the SQLite file. So what you can do is change the file name of the file being imported so you can lift it from the bundle. I haven't tried it myself but it should work. In you AppDelegate you would have something like this: `NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"APPNAME.sqlite"];` ; You can be chang it to `NSString *path = [[NSBundle mainBundle] pathForResource:@"APPNAME" ofType:@"sqlite" inDirectory:@"/"];` – thesecondlastjedi Sep 14 '12 at 09:05
  • Also check this out from the related section: [Core Data data schema from existing SQLite database](http://stackoverflow.com/a/6388697/1400225) – thesecondlastjedi Sep 14 '12 at 09:14
0

You would do well to listen to the advice here. Given that the internal database is decorated, you would at least have to do the same with your own database to make it even have half a chance of working.

As previously stated, you should import it, for example, within your code base with some one time code written for the task. That way, ios will take care of everything for you natively.

Lloyd Moore
  • 3,117
  • 1
  • 32
  • 32