KQLite INSERT

KQLite provides following ways to insert data into KQLiteTable.

Quickly inserting a row into KQLiteTable

KQLiteTable provides the quickInsert extension method to simplify inserting a single row into a table avoiding complex query execution manually. The method does not return anything for operations chaining. Database must be open before performing any operation.

val myDatabase = MyDatabase()
myDatabase.open()

Artists.quickInsert {  it: Artists 
    it.artistId.bind(1)
    it.artistName.bind("SuperCoolMan")
}

myDatabase.close()

Inserting multiple rows using prepared statement

val prepared = Artists.insert(Artists.artistName)

// Bind and execute to insert multiple rows
for (i in 2..5) {
    prepared.bind {  it: Artists 
        it.artistName.bind("SuperCoolMan $i")
    }.execute()
}

prepared.close() // Close once done

Inserting into all columns without specifying column names

val prepared = Artists.insert()

// Insert multiple rows
for (i in 6..8) {
    prepared.bind {  it: Artists 
        it.artistId.bind(i)
        it.artistName.bind("SuperCoolMan $i")
    }.execute()
}

prepared.close() // Close once done

Inserting multiple rows by VALUES

val prepared = Artists.insert()
prepared.values(9, "SuperCoolMan 9").execute()
prepared.values(10, "SuperCoolMan 10").execute()
prepared.close()

Fetching newly inserted ROWID by RETURNING

val prepared = Artists.insert()

val id: Long = prepared.bind {  it: Artists 
    it.artistName.bind("SuperCoolMan 11")
}.executeReturning(Artists.rowId)

println("Inserted artist with rowId: $id")
prepared.close()

Fetching multiple columns by RETURNING

val prepared = Artists.insert()

val cursor: KQLiteCursor = prepared.bind {  it: Artists 
    it.artistName.bind("SuperCoolMan 12")
}.executeReturning(Artists.artistId, Artists.artistName)

println(
    "Artist inserted: id=${cursor[Artists.artistId]}, name=${cursor[Artists.artistName]}"
)

prepared.close()

Fetching newly inserted row by RETURNING *

val prepared = Artists.insert()

val cursor: KQLiteCursor = prepared.bind {  it: Artists 
    it.artistName.bind("SuperCoolMan 13")
}.executeReturning() // Empty returning means *

println(
    "Artist inserted: id=${cursor[Artists.artistId]}, name=${cursor[Artists.artistName]}"
)

prepared.close()

Insert by SELECT from another table

Artists
    .insert(Artists.artistName)
    .select {
        Employees
            .select(
                Employees.firstName CONCAT " " CONCAT Employees.lastName
            )
            .where {  it: Employees 
                it.title LIKE "%artist%"
            }
    }.execute()

Insert binding functions

Artists.quickInsert {  it: Artists 
    it.artistName.bind(UPPER(StringLiteral("SuperCoolMan")))
}

Inserting DEFAULT values

Artists.insertDefaultValues()
⬅ Defining Database KQLite SELECT ⮕