开发者

Access .sqlite file through Core Data API

开发者 https://www.devze.com 2023-03-30 16:29 出处:网络
I am new to Core Data and i got stuck with a problem. I have a file \"db.sqlite\" which has 3 tables and each containing around 10k records.

I am new to Core Data and i got stuck with a problem.

I have a file "db.sqlite" which has 3 tables and each containing around 10k records.

Now my question is can I add this file to my project and access(Select/Update) the data using CoreData API.

T开发者_如何学Gohank You in advance.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消