
Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 19, 2024
In this tutorial, we’ll discuss the SQLDelight library by Cash App. SQLDelight is a powerful database library made specifically for Kotlin-based projects. This library simplifies the process of interacting with databases by providing a type-safe, compile-time approach. It also allows developers to write SQL queries directly to our code.
Let’s look at a few reasons why we should consider using SQLDelight:
Let’s review the steps involved in integrating SQLDelight into a Kotlin project.
First, we’ll need to add a couple of dependencies to our project’s build.gradle file:
implementation("com.squareup.sqldelight:runtime:$version")
implementation("com.squareup.sqldelight:jdbc-driver:$version")
kapt("com.squareup.sqldelight:gradle-plugin:$version")
Or, if we’re using Maven, we’ll add them to our pom.xml file:
<dependency>
<groupId>com.squareup.sqldelight</groupId>
<artifactId>runtime</artifactId>
<version>version</version>
</dependency>
<dependency>
<groupId>com.squareup.sqldelight</groupId>
<artifactId>kotlin-driver</artifactId>
<version>version</version>
</dependency>
Remember to replace the variable version with the latest stable version of the SQLDelight library from Cash App.
We define our database schema using SQL syntax by creating a file ending with the .sq suffix. Afterward, we specify the tables, columns, and queries we need for our application.
Let’s begin our .sq file by creating our schema — for this article, it’s a single table:
CREATE TABLE user (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL,
email_address TEXT
);
After we define the table schema, it is advisable to always build or rather sync our project to ensure the SQLDelight code generation process takes place. Once the code is generated, we can use the generated table class to write queries to our database.
Next, let’s write a few queries in our SQLDelight .sq file for our database:
SelectAll:
SELECT * FROM user;
SelectUserById:
SELECT * FROM user WHERE id = ?;
InsertUser:
INSERT INTO user(first_name, last_name, email_address)
VALUES (?, ?, ?);
DeleteUser:
DELETE FROM contact WHERE id = ?;
In our example, after we build our project, the generated code for our model will look like:
String CREATE_TABLE = ""
+ "CREATE TABLE user (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n"
+ " first_name TEXT NOT NULL, last_name TEXT NOT NULL,\n"
+ " email_address TEXT)";
String SELECTALL = ""
+ "SELECT * FROM user";
String SELECTUSERBYID = ""
+ "SELECT * FROM user WHERE id = ?";
long id();
@NonNull
String first_name();
@NonNull
String last_name();
@Nullable
String email_address();
Despite SQLDelight being a great library to work with while dealing with databases, it’s important to always adhere to a few guidelines to ensure that we don’t create simple mistakes in our codebase.
First, we should always be keen to ensure that the schema definition accurately reflects the structure of the database tables, columns, indexes, and constraints. If possible, it’s advisable to avoid changing the generated code since any modifications made to our generated files will be overwritten during our next sync.
We should also be conscious not to write complex and unnecessary subqueries to our codebase, as these may impact its performance.
Additionally, runtime errors may occur due to database-related issues. We should ensure proper error handling and communicate errors effectively to our users. If possible, it’s best to always include tests for edge cases, such as handling null values or testing complex queries involving multiple tables.
Finally, whenever we modify the database schema, we must plan and execute proper database migrations to avoid data loss or application crashes.
In this article, we went through the SQLDelight library by the Cash App team, which is simply a library to enable us to interact with databases by providing a type-safe, compile-time approach. We also discussed how we could integrate the SQLDelight library into our project, create tables, provide database instances, and write queries.