A batch consists of multiple SQL statements executed sequentially within an
implicit transaction. The backend handles the transaction: success commits all
changes, while any failure results in a full rollback with no modifications.
Copy
db.connect().use { it.execute_batch(" CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY, name TEXT NOT NULL ); INSERT INTO users (name) VALUES ('Alice'); INSERT INTO users (name) VALUES ('Bob'); ")}
Interactive transactions in SQLite ensure the consistency of a series of read
and write operations within a transaction’s scope. These transactions give you
control over when to commit or roll back changes, isolating them from other
client activity.
Copy
db.connect().use { val tx = it.transaction(); tx.execute("INSERT INTO users (name) VALUES (?)", "Iku"); tx.execute("INSERT INTO users (name) VALUES (?)", "Iku 2"); tx.commit() // or tx.rollback()}