SQLDelight in Android: Getting Started

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

Part 1: Preparation & Setup

08. Utilize Transactions & Rollbacks

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: 07. Use Grouping Statements Next episode: 09. Write Migrations for Your Database

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've reached locked video content where the transcript will be shown as obfuscated text.

Aside from the other wonderful features that we have explored so far, SQLDelight also supports one of the most fundamental concepts of working with SQL databases: Transactions.

addBugToCollection:
INSERT INTO inCollection(collectionId, bugId, quantity)
VALUES (:collectionId, :bugId, :quantity);
fun addBug(
+    collectionId: Long,
    name: String,
    description: String?,
    size: String,
    weight: String,
    attack: Int,
    defense: Int,
    quantity: Int
) {
    database.bugQueries.insert(name, description, size, weight, attack, defense)
}
fun addBug(
    collectionId: Long,
    name: String,
    description: String?,
    size: String,
    weight: String,
    attack: Int,
    defense: Int
) {
+    database.bugQueries.transaction {
+
+    }
    database.bugQueries.insert(name, description, size, weight, attack, defense)
}
    database.bugQueries.transaction {
+        afterRollback {
+        }
+        afterCommit {
+        }
-        afterRollback {
-        }
-        afterCommit {
-        }
    }
database.bugQueries.transaction {
+    database.bugQueries.insert(name, description, size, weight, attack, defense)
}
-database.bugQueries.insert(name, description, size, weight, attack, defense)
fun addBug(
    collectionId: Long,
    name: String,
    description: String?,
    size: String,
    weight: String,
    attack: Int,
    defense: Int,
+    quantity: Int
) {
    database.bugQueries.transaction {
        database.bugQueries.insert(name, description, size, weight, attack, defense)

+        database.inCollectionQueries.addBugToCollection(
+            collectionId = collectionId,
+            bugId = ...,
+            quantity = quantity
        )
    }
}
getLastInsertedId:
SELECT last_insert_rowid();
fun addBug(
    collectionId: Long,
    name: String,
    description: String?,
    size: String,
    weight: String,
    attack: Int,
    defense: Int,
    quantity: Int
) {
    database.bugQueries.transaction {
        database.bugQueries.insert(name, description, size, weight, attack, defense)

+        val bugId = database.bugQueries.getLastInsertedId().executeAsOne()

        database.inCollectionQueries.addBugToCollection(
            collectionId = collectionId,
+            bugId = bugId,
            quantity = quantity
        )
    }
}
fun addBug() {
    // Generate some random data for the bug, then add it
    val bug = Random.nextBug()

    repository.addBug(
        collectionId = collectionId,
        name = bug.name,
        description = bug.description,
        size = bug.size,
        weight = bug.weight,
        attack = bug.atk,
        defense = bug.def,
        quantity = 1
    )
}