androidx.sqlite SQLite Room
SQLite SQLite
KMP SQLite build.gradle.kts
androidx.sqlite 2.5.0 KMPWeb JavaScript
WasmJS 2.7.0 [versions]
sqlite = "2.7.1"
[libraries]
# The SQLite Driver interfaces
androidx-sqlite = { module = "androidx.sqlite:sqlite", version.ref = "sqlite" }
# The bundled SQLite driver implementation
androidx-sqlite-bundled = { module = "androidx.sqlite:sqlite-bundled", version.ref = "sqlite" }
[plugins]
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
SQLite API
androidx.sqlite
SQLite API API
androidx.sqlite:sqlite-bundled androidx.sqlite:sqlite-framework Android iOS API SQLite C API
SQLiteDriverSQLiteSQLiteConnectionsqlite3SQLiteStatementsqlite3_stmt
API
fun main() {
val databaseConnection = BundledSQLiteDriver().open("todos.db")
databaseConnection.execSQL(
"CREATE TABLE IF NOT EXISTS Todo (id INTEGER PRIMARY KEY, content TEXT)"
)
databaseConnection.prepare(
"INSERT OR IGNORE INTO Todo (id, content) VALUES (? ,?)"
).use { stmt ->
stmt.bindInt(index = 1, value = 1)
stmt.bindText(index = 2, value = "Try Room in the KMP project.")
stmt.step()
}
databaseConnection.prepare("SELECT content FROM Todo").use { stmt ->
while (stmt.step()) {
println("Action item: ${stmt.getText(0)}")
}
}
databaseConnection.close()
}
SQLite C API
-
SQLiteDriver -
SQLiteConnection.prepareSQL -
SQLiteStatement-
bind* -
step -
get*
-
AndroidSQLiteDriver |
androidx.sqlite:sqlite-framework |
Android |
NativeSQLiteDriver |
androidx.sqlite:sqlite-framework |
iOSMac Linux |
BundledSQLiteDriver |
androidx.sqlite:sqlite-bundled |
AndroidiOSMacLinux JVM |
WebWorkerSQLiteDriver |
androidx.sqlite:sqlite-web |
JavaScript WebAssembly (WasmJS) |
androidx.sqlite:sqlite-bundled BundledSQLiteDriver
SQLite KMP
SQLite Room
API SQLite SQLite Room
A RoomDatabase SQLiteDriver
RoomDatabase.Builder.setDriverRoom
RoomDatabase.useReaderConnection
RoomDatabase.useWriterConnection
Kotlin Multiplatform
SQLite API SupportSQLiteDatabase SQLite
Kotlin Multiplatform
SQLiteConnection
RoomDatabase API
API androidx.sqlite
API val connection: SQLiteConnection = ...
connection.execSQL("BEGIN IMMEDIATE TRANSACTION")
try {
// perform database operations in transaction
connection.execSQL("END TRANSACTION")
} catch(t: Throwable) {
connection.execSQL("ROLLBACK TRANSACTION")
}
val connection: SQLiteConnection = ...
connection.execSQL("ALTER TABLE ...")
val connection: SQLiteConnection = ...
connection.prepare("SELECT * FROM Pet").use { statement ->
while (statement.step()) {
// read columns
statement.getInt(0)
statement.getText(1)
}
}
connection.prepare("SELECT * FROM Pet WHERE id = ?").use { statement ->
statement.bindInt(1, id)
if (statement.step()) {
// row found, read columns
} else {
// row not found
}
}
Android
SupportSQLiteDatabase
RoomDatabase API
API androidx.sqlite
API val database: SupportSQLiteDatabase = ...
database.beginTransaction()
try {
// perform database operations in transaction
database.setTransactionSuccessful()
} finally {
database.endTransaction()
}
val database: SupportSQLiteDatabase = ...
database.execSQL("ALTER TABLE ...")
val database: SupportSQLiteDatabase = ...
database.query("SELECT * FROM Pet").use { cursor ->
while (cursor.moveToNext()) {
// read columns
cursor.getInt(0)
cursor.getString(1)
}
}
database.query("SELECT * FROM Pet WHERE id = ?", id).use { cursor ->
if (cursor.moveToNext()) {
// row found, read columns
} else {
// row not found
}
}