SQLite: The C Library

Episode #301 • Nov 4, 2024 • Subscriber-Only

SQLite is one of the most well-crafted, battle-tested, widely-deployed pieces of software in history, and it’s a great fit for apps with more complex persistence needs than user defaults or a JSON file. Let’s get familiar with the library, starting with a crash course in interacting with C code from Swift.

Previous episode
SQLite: The C Library
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

Brandon

Today we are embarking on a series of episodes to show how to persist data in your applications using SQLite. SQLite is one of the most well-crafted and battle-tested pieces of software in the history of software. It’s installed on tens of billions of devices worldwide, and its test suite runs over 500 million tests every day!

However, SQLite is a C library, and C is a language that does not have many of the niceties that we have come to expect from our experience with Swift. There’s a very limited type system, no enums, no generics, and it has pervasive use of unsafe pointers. However, Swift has great interoperability with C and so with a little bit of work we can very easy call into any of SQLite’s functionality.

Stephen

In the series we are going to accomplish 3 main goals:

  • First we are going to give a crash course in calling into C code from Swift, including how one deals with all the unsafe pointers that are necessary when dealing with C code. And along the way we will learn the basics of SQLite, including how to create a database, how to create a table, how to insert data into the table, and how to query the database.

Brandon
  • Then we will show how to use one of the most popular 3rd party libraries for putting a nice Swift interface on top of SQLite, which is GRDB by Gwendal Roué. This gives you all the basic tools for interacting with a database, such as creating tables, insert data and querying, but with some additional powers for subscribing to changes and modeling relationships between models.

Stephen
  • And the finally we will explore some advanced topics in SQLite and GRDB by showing off how to display data from the database in SwiftUI, how to observe changes to the database so that the view updates automatically when data is updated, and more.

Brandon

But, let’s start from the very beginning. Let’s see what it takes to interact with the SQLite C library that automatically comes on every iOS and macOS device.

SQLite basics


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