Set up a Kotlin Multiplatform SQLDelight database

Learn how to set up a Kotlin Multiplatform SQLDelight database with CRUD operations, migrations, and platform-specific drivers.

Posted by

Set up a Kotlin Multiplatform SQLDelight database
TL;DR
  • This article explains how to set up a local database using SQLDelight in Kotlin Multiplatform.
  • You will learn about shared schema definitions, CRUD operations, and platform-specific drivers.
  • Follow the step-by-step guide to implement a fully functional cross-platform database.

How does SQLDelight work in Kotlin Multiplatform?

SQLDelight is a type-safe Kotlin multiplatform SQL library that generates Kotlin APIs from SQL statements. It allows developers to define their database schema in SQL and automatically generates the necessary code for accessing the database in a type-safe manner. This makes it an excellent choice for building a local database in Kotlin Multiplatform projects.

Why use SQLDelight for a KMP local database?

Using SQLDelight for a KMP local database provides several benefits, including type safety, ease of migration, and the ability to write platform-specific code. SQLDelight generates Kotlin APIs based on your SQL schema, which reduces runtime errors and simplifies the development process. Additionally, it offers reactive support with Kotlin Flows, making your database queries reactive and efficient.

How to set up SQLDelight in your Kotlin Multiplatform project?

Setting up SQLDelight in a Kotlin Multiplatform project involves several steps. Here’s a step-by-step guide to get you started:
  1. Create a new Kotlin Multiplatform project
    Use the Kotlin Multiplatform project template in your IDE or create a project structure manually by defining the shared, android, and ios modules.
  2. Add SQLDelight dependencies
    In your build.gradle.kts file, add the following SQLDelight dependencies:
    kotlin
    plugins { kotlin(

Build your KMP app faster

Skip the setup and start shipping with a production-ready Kotlin Multiplatform starter kit.