11

Is it possible to generate a .xcdatamodel (CoreData data model) from an existing SQLite database file?

I've developed a SQLite database and written a Java API against it. Now, I need to write an equivalent iOS API to the same database (file). I'd prefer not to create the data schema by hand in xCode. I just want to generate the .xcdatamodel, and use xCode to generate the entity classes that I'll code against.

Is it possible?

2
  • 1
    @Big_Chair I’m not sure what updated answer you’re hoping for. Core Data is can use SQLite as a data store, but you can’t use Core Data as a front end to an SQLite database that you’ve already created, which is what the OP seemed to want, and that hasn’t changed in the past 10 years. IOW, If you create a Core Data model for the entities in some db, with appropriate connections between them, and then tell Core Data to use SQLite as the backing store, the SQLite database that Core Data creates might not look much at all like the database you started with. Commented Aug 14, 2021 at 17:42
  • 1
    @Caleb Yeah I had to realize that CoreData was not fit for my requirements and I opted for using GRDB.swift instead. I was looking for something similar as Room on Android and assumed it must be CoreData, but it seems it has a different focus. Thanks for the reply, though. Commented Aug 15, 2021 at 8:02

3 Answers 3

9

See How do I use my existing SQLite database with Core Data? and Efficiently Importing Data. I understand that you're not asking specifically about using the entire database but just creating the model, but you have to understand that there's not a 1-1 relationship between an existing SQLite database and what Core Data wants to store.

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

1 Comment

I understand what you're saying, but it sure would be nice to be able to programmatically automate the process of creating the initial cut of the .xcdatamodeld, rather than having to manually create it. With a complex database, that's not a trivial issue.
5

Try the following tool which wil generate the Datamodel from existing Sqlite

https://github.com/tapasya/Sqlite2CoreData

1 Comment

Does anyone actually used this frequently to build a Core Data for a relatively large SQLite DB?
3

No. Core Data is not just a thin wrapper around SQLite. It's an object store that can (optionally) be persisted to a SQLite database. This means that Core Data models do not have a direct mapping to the SQLite database.

In your case I'd recommend using one of the SQLite wrappers that are available (I've not used any of them so I couldn't recommend any one in particular).

Comments

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.