SQLite: GRDB

Episode #302 • Nov 11, 2024 • Subscriber-Only

Interfacing with SQLite’s C library from Swift is possible, but clunky. Luckily there are friendlier, “Swiftier” interfaces the community has built, so let’s take a look at the most popular: GRDB. We’ll explore how it can help us avoid pitfalls and boilerplate required to use the C library, and how its typed SQL helpers can even help us avoid runtime issues at compile time.

Previous episode
SQLite: GRDB
Locked

Unlock This Episode

Our Free plan includes 1 subscriber-only episode of your choice, plus weekly updates from our newsletter.

Sign in with GitHub

Introduction

Stephen

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.

Brandon

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.

GRDB


References

Downloads

Sample code

Get started with our free plan

Our free plan includes 1 subscriber-only episode of your choice, access to 64 free episodes with transcripts and code samples, and weekly updates from our newsletter.

View plans and pricing