1


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.

Thank You in advance.

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

6 Comments

+1 @Mundi - Thank you very much for your answer. If i want only to read the data i.e.,(Select) can i do it with CoreData?
I do not think so. You will have to use the SQLite API. You process the dictionary of each row, transform it into a core data entity and save.
Core Data will only read an SQL file using the Core Data schema and generated with the .xcdatamodel provided to the persistent store coordinator. You can't use it read an arbitrary sql file. That is not what Core Data is for.
+1 @Tech Zen - Then can we follow this: 1. Import all data into CoreData's database using sqlite API 2. Now use CoreData API to fetch and update database. I tend to use CoreData because accessing will be faster and paging will be handled (We no need to worry on Memory Mgmt.)
+1 @Mundi - Inorder to do that we should know the Data Model. Right? (I am asking this because my sqlite database schema changes if there are any server side changes).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.