<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Ezichi Amarachi</title>
    <description>The latest articles on Forem by Ezichi Amarachi (@czech).</description>
    <link>https://forem.com/czech</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F275359%2Fcc32b3d5-56a0-4294-96cc-b4cabe7e2431.png</url>
      <title>Forem: Ezichi Amarachi</title>
      <link>https://forem.com/czech</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/czech"/>
    <language>en</language>
    <item>
      <title>Setting up an SQLDelight database on your android application using Kotlin</title>
      <dc:creator>Ezichi Amarachi</dc:creator>
      <pubDate>Tue, 14 Jun 2022 10:12:52 +0000</pubDate>
      <link>https://forem.com/czech/setting-up-an-sqldelight-database-on-your-android-application-using-kotlin-1l08</link>
      <guid>https://forem.com/czech/setting-up-an-sqldelight-database-on-your-android-application-using-kotlin-1l08</guid>
      <description>&lt;p&gt;There are a number of options to choose from when looking to have a database on your android application. In this article, we will be setting up an SQLDelight database in your already existing android application. To follow along with this article, you should have a basic knowledge of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Android and Android Studio&lt;/li&gt;
&lt;li&gt;Kotlin&lt;/li&gt;
&lt;li&gt;Hilt for dependency injection&lt;/li&gt;
&lt;li&gt;SQL&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Room database is a database framework in the Android Architecture component and is usually the go-to option for android developers. It wraps SQLite to allow for robust database access while still providing the full power of SQLite.&lt;/p&gt;

&lt;p&gt;SQLDelight, on the other hand, is a database framework developed by Square. It generates type-safe Kotlin APIs from your SQL statements. The premise of SQLDelight is: Write SQLite code and let the Gradle plugin generate APIs to run your queries for you. &lt;/p&gt;

&lt;p&gt;Some of the pros of SQLDelight is that it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;generates type-safe code from the SQL statements.&lt;/li&gt;
&lt;li&gt;provides IDE features that make writing and maintaining SQL 
easy.&lt;/li&gt;
&lt;li&gt;has cross-platform (compatible with KMM) support which means 
it can be used in both Android and iOS.&lt;/li&gt;
&lt;li&gt;is better when dealing with multi-table databases.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I prefer it to Room database because i think it's easier to set up and use, but that's just me. &lt;/p&gt;

&lt;p&gt;Some of its cons are that it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;involves writing more SQL code than you would when using 
Room database.&lt;/li&gt;
&lt;li&gt;is rumoured to be slower than Room database.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Well, let's get into it!&lt;/p&gt;

&lt;p&gt;The first thing we'll need to do is to install the SQLDelight plugin on android studio. Navigate to the 'plugins' section on Android Studio preferences, search for "sqldelight" and install the plugin. You would need to restart AS after the install is complete.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2bq8yodqoixod38zv39k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2bq8yodqoixod38zv39k.png" alt="preferences &amp;gt; plugins &amp;gt; marketplace"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For the next step, we'll go ahead to add the necessary dependencies. First we have to add the SQLDelight gradle plugin in our project's top-level &lt;code&gt;build.gradle&lt;/code&gt; file as so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath("com.squareup.sqldelight:gradle-plugin:1.5.3")
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next we'll apply the gradle plugin in our app or module's &lt;code&gt;build.gradle&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apply plugin: 'com.squareup.sqldelight'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above line of code can also be written as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;plugins {
    id 'com.squareup.sqldelight'
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Still in our module's &lt;code&gt;build.gradle&lt;/code&gt; file, we'll add the following dependencies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dependencies {
   // SQLDelight
    implementation "com.squareup.sqldelight:runtime:1.5.3"
    implementation "com.squareup.sqldelight:android-driver:1.5.3"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then sync the project.&lt;/p&gt;

&lt;p&gt;Next, we'll need to create a file that'll hold our SQL statements. This file has to be defined in the &lt;code&gt;main&lt;/code&gt; package. For this, it is recommended to switch from &lt;code&gt;Android&lt;/code&gt; view to &lt;code&gt;Project&lt;/code&gt; view. Right-click the main package and create a directory named &lt;code&gt;sqldelight&lt;/code&gt;, then add two other directories as so: &lt;code&gt;sqldelight/com/czech/androidparty/cache&lt;/code&gt;. Right-click on the &lt;code&gt;cache&lt;/code&gt; file and create a new &lt;code&gt;.sq&lt;/code&gt; file as shown below.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv5rrqpw05y6tbu5q6t5o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv5rrqpw05y6tbu5q6t5o.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I'll name mine &lt;code&gt;AndroidPartyDB.sq&lt;/code&gt;.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2j9f7qyxo1nkbuxvebvg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2j9f7qyxo1nkbuxvebvg.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that we have the &lt;code&gt;.sq&lt;/code&gt; file that'll house our SQL statements, let's proceed to writing said statements in the file. The first statement in the SQL file creates a table. In the file we'll write the code below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE Data_Entity(
    distance INTEGER NOT NULL PRIMARY KEY,
    name TEXT NOT NULL
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;_NB&lt;/strong&gt;: The PRIMARY KEY constraint uniquely identifies each record in a table. Primary keys must contain UNIQUE values, and cannot contain NULL values. A table can have only ONE primary key; every table can have (but does not have to) a primary key._ &lt;/p&gt;

&lt;p&gt;SQLDelight will generate a &lt;code&gt;Data_Entity&lt;/code&gt; data class which represents our table and a &lt;code&gt;Database&lt;/code&gt; class with a &lt;code&gt;Schema&lt;/code&gt; object which we will use to create the database and run statements on it. In the spirit of clean code, we will be creating an instance of the &lt;code&gt;Schema&lt;/code&gt; object and using it to create our database in separate classes. This is also because we want to provide our database in a module using Hilt (for dependency injection). Let's write some code!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class DriverFactory(private val context: Context) {

    fun createDriver(): SqlDriver {
        return AndroidSqliteDriver(Database.Schema, context, "androidParty.db")
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above block is our &lt;code&gt;DriverFactory&lt;/code&gt; class where we have created an instance of our database Schema object in the &lt;code&gt;createDriver()&lt;/code&gt; function. We will use this class in the next block of code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class AndroidPartyDatabaseFactory(private val driverFactory: DriverFactory) {

    fun createDriver(): Database {
        return Database(
            driver = driverFactory.createDriver()
        )
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, the &lt;code&gt;AndroidPartyDatabaseFactory&lt;/code&gt; class takes in the &lt;code&gt;DriverFactory&lt;/code&gt; class as a parameter which is used to pass the &lt;code&gt;createDriver()&lt;/code&gt; function as the value of our database &lt;code&gt;driver&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;We have created our database. Before we move further, we will add labeled statements to our &lt;code&gt;.sq&lt;/code&gt; file. For each labeled statement, a type-safe function which will be available at runtime will be generated. Files with labeled statements in them will have a queries file generated for them matching the name of your &lt;code&gt;.sq&lt;/code&gt; file (eg: for our &lt;code&gt;AndroidPartyDB.sq&lt;/code&gt; file, an &lt;code&gt;AndroidPartyDBQueries&lt;/code&gt; file will be generated). This queries file will be used to perform database operations. So, inside your &lt;code&gt;.sq&lt;/code&gt; file, add the following SQL code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;insertData:
INSERT OR REPLACE
INTO Data_Entity(
    name,
    distance
) VALUES (?, ?);

getData:
SELECT  * FROM Data_Entity;

deleteData:
DELETE FROM Data_Entity;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As the names imply, &lt;code&gt;insertData&lt;/code&gt; adds new data to the database, &lt;code&gt;getData&lt;/code&gt; fetches already saved data and &lt;code&gt;deleteData&lt;/code&gt; deletes data from the database. &lt;br&gt;
Now rebuild the project to generate the needed files.&lt;/p&gt;

&lt;p&gt;Next, we will create an interface containing functions for all the labels we added to the &lt;code&gt;.sq&lt;/code&gt; file above:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface AndroidPartyCache {

    fun insertData(data: DataList)

    fun insertData(dataList: List&amp;lt;DataList&amp;gt;)

    fun getData(): List&amp;lt;DataList&amp;gt;

    fun deleteData()
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;DataList&lt;/code&gt; is a serializable &lt;code&gt;Data Class&lt;/code&gt; holding all the entries in our database&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Serializable
class DataList(
    val name: String,
    val distance: Int
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The next step is to create an implementation class for the above interface but before we do that, we need to provide our &lt;code&gt;AndroidPartyDatabaseFactory&lt;/code&gt; with hilt because we will be injecting it into the class. For this we will create a new hilt Module and provide our database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Module
@InstallIn(SingletonComponent::class)
object CacheModule {

    @Provides
    @Singleton
    fun provideAndroidPartyDatabaseFactoryDriver(
        context: BaseApplication
    ): Database {
        return AndroidPartyDatabaseFactory(
            driverFactory = DriverFactory(context = context)
        ).createDriver()
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's create a new kotlin class which implements the &lt;code&gt;AndroidPartyCache&lt;/code&gt; interface and inject the &lt;code&gt;Database&lt;/code&gt; already provided with hilt:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class AndroidPartyCacheImpl @Inject constructor(
    database: Database
): AndroidPartyCache {

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside the class, create a &lt;code&gt;queries&lt;/code&gt; variable to get the generated &lt;code&gt;Database&lt;/code&gt; queries as so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private val queries: AndroidPartyDBQueries = database.androidPartyDBQueries
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We'll override the functions of the parent interface and perform the necessary operations inside them. At the end, our &lt;code&gt;AndroidPartyCacheImpl&lt;/code&gt; will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class AndroidPartyCacheImpl @Inject constructor(
    database: Database
): AndroidPartyCache {

    private val queries: AndroidPartyDBQueries = database.androidPartyDBQueries

    override fun insertData(data: DataList) {
        queries.insertData(
            name = data.name,
            distance = data.distance.toLong()
        )
    }

    override fun insertData(dataList: List&amp;lt;DataList&amp;gt;) {
        for (data in dataList) {
            insertData(data)
        }
    }

    override fun getData(): List&amp;lt;DataList&amp;gt; {
        return queries.getData().executeAsList().toDataList()
    }

    override fun deleteData() {
        return queries.transaction {
            queries.deleteData()
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A couple of things to note:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In the first &lt;code&gt;insertData()&lt;/code&gt; function, the &lt;code&gt;distance&lt;/code&gt; entry is converted to a long using &lt;code&gt;toLong()&lt;/code&gt; because the &lt;code&gt;INTEGER&lt;/code&gt; type in our SQL is converted to a type &lt;code&gt;Long&lt;/code&gt; when a kotlin version of our &lt;code&gt;Data_Entity&lt;/code&gt; table is created so we can only insert a Long.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In &lt;code&gt;getData()&lt;/code&gt;, &lt;code&gt;executeAsList()&lt;/code&gt; is used to convert &lt;code&gt;List&amp;lt;Data_Entity&amp;gt;&lt;/code&gt; to a mutable list, it is a generated function. &lt;code&gt;toDataList()&lt;/code&gt; is an extension function used to convert a &lt;code&gt;List&amp;lt;Data_Entity&amp;gt;&lt;/code&gt; back to &lt;code&gt;List&amp;lt;DataList&amp;gt;&lt;/code&gt; when fetching data from the database as seen below:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;object SqlConverter {

    private fun Data_Entity.toData(): DataList {
        return DataList(
            name = name,
            distance = distance.toInt()
        )
    }

    fun List&amp;lt;Data_Entity&amp;gt;.toDataList(): List&amp;lt;DataList&amp;gt; {
        return map { it.toData() }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, we can provide the &lt;code&gt;AndroidPartyCache&lt;/code&gt; with hilt so it can be injected wherever we want to use it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Provides
    @Singleton
    fun provideAndroidPartyCache(
        database: Database
    ): AndroidPartyCache {
        return AndroidPartyCacheImpl(
            database = database
        )
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's about it! Thanks for sticking with me and congrats on making it to the end. I hope you find this article helpful. &lt;br&gt;
The complete project can be found &lt;a href="https://github.com/Czeach/AndroidParty" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Cheers! :)&lt;/p&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
    </item>
  </channel>
</rss>
