Reputation: 39
I'm pretty new to swift coding, but I'm working on an app which really needs a database for a large amount of data. I've looked into coredata and it doesn't seem to be what I need. I full database that works with sql statements is really what I'm looking for.
There's plenty of information on the web about setting up an sqlite database within iOS but it looks like things might have changed since Swift 3 and xcode 8 have come out.
I'm not sure how to actually 'connect' to an sqlite database from within the project, all the code I've seen requires a 'path' to the database which is on the computer, but when the app is shipped, users obviously aren't going to be able to access that path. Do you need to create a .sqlite file within the project?
Thanks for your help!
Upvotes: 4
Views: 11434
Reputation: 4054
You surely need some database file, and that database file has a path. Even on iOS.
Now if your application users can modify the database, then it's likely that the database file is stored inside the Documents folder, a special folder that all iOS apps have. Your create a path to a file inside this folder and open the database at that path. SQLite will create an empty database if needed.
If your application users should not modify the database (for an English dictionary app for example), then the database is likely a resource of your application. Resources are files bundled within your application. You ask NSBundle.main for the path of this resource, and open a connection to it.
You can use GRDB.swift library in you need support: https://github.com/groue/GRDB.swift. It supports Swift 2.2 / 2.3 / 3.0.
Upvotes: 2