And so that is the basics of interacting with the SQLite C library from Swift. We now know how to create a database connection, create tables, insert data into tables, and query tables for data. And really this was also a lesson in how one interacts with C libraries from Swift, in general. Long story short: it ain’t pretty. But it is also amazing that it is possible at all.
And because of the pain we witnessed when trying to interact with a C library, it’s no surprise that many people have tried to put a friendlier and “Swiftier” interface over SQLite. There are a number of libraries out there, but no doubt the most popular is GRDB by Gwendal Roué. It’s a very well-maintained project, it’s been around for a long time, and we don’t think anyone should have qualms about adding it as a dependency if you need to interact with SQLite.
So, let’s import GRDB into our project and see what it takes to create database connections, create tables, insert data into tables, and query tables. And we will also show off a couple of advanced features too, such as subscriptions and relations.