Access .sqlite file through Core Data API
You can't use Core Data to read an arbitrary SQL file. Neither can you reliable convert an arbitrary SQL file into an SQL file that Core Data can use. The sqlite schema for Core Data is undocumented and ever changing. Core Data will only read and write its schema and each each sqlite store file must be configured to a specific data model file. Direct creation of a SQL store for Core Data is always a fragile solution.
It is important to understand that Core Data is not SQL. Entities are not tables. Objects are not rows. Attributes are not columns. Relationships are not joins. Core Data is an object graph management system that may or may not persist the object graph and may or may not use SQL far behind the scenes to do so. Trying to think of Core Data in SQL terms will cause you to completely misunderstand Core Data and result in much grief and wasted time.
As Mundi pointed out, you need to read the existing SQL file in using the standard SQL C API and then load that data in managed objects and have Core Data save the objects into its SQL store. It might sound like a lot of extra work but in reality you are translating from the limited SQL API which is concerned solely with getting data on and off disk to the Core Data API which provides a complete data model for your app.
The Core Data API actually hides the mechanics of the so-called persistent store. It is more of an object modeling API that can persist data very efficiently. It could also use a database different from sqlite.
Thus, if you want to use Core Data, I would recommend first importing all the records and creating a new database file with Core Data. You can use the SQLite API to read the existing database and the Core Data API to write out the new object oriented data into the new database.
Once all of your original data is in Core Data, you should stick with that.
精彩评论