SQLDelight in Android: Getting Started

Aug 3 2021 · Kotlin 1.4, Android 11, Android Studio 4.1

Part 1: Preparation & Setup

04. Instantiate the Database

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 03. Add Tables to the Database Next episode: 05. Understand SQLDelight's Type System

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

We have now declared the contents of our database by means of the script files and SQLDelight created classes each table. However, it also generated something else - a Database class! This is how we will access the tables from our codebase, so now is a good time to connect the database to the rest of our app.

-class DatabaseRepository
+class DatabaseRepository(driver: SqlDriver)
-class DatabaseRepository(driver: SqlDriver)
+class DatabaseRepository(driver: SqlDriver) {
+    private val database = Database(driver)
+}
databaseRepository = DatabaseRepository(
+    AndroidSqliteDriver(
+       schema = Database.Schema,
+       context = this,
+       name = "bugs.db",
+    )
)
databaseRepository = DatabaseRepository(
    AndroidSqliteDriver(
       schema = Database.Schema,
       context = this,
       name = "bugs.db",
+      callback = object : AndroidSqliteDriver.Callback(Database.Schema) {
+           override fun onConfigure(db: SupportSQLiteDatabase) {
+               super.onConfigure(db)
+               db.setForeignKeyConstraintsEnabled(true)
+           }
+      }
    )
)