InsertStatement

Represents a prepared SQL INSERT statement that can be configured and executed.

An InsertStatement is created by calling the insert function on a KQLiteTable object. It provides a fluent API to specify the values to be inserted, either through a bind block, a values list, or a select statement.

This interface is AutoCloseable and should be used within a use block to ensure that the underlying SQLiteStatement is closed properly, preventing resource leaks.

Usage Examples:

Using bind:

Artists.insert().use { statement ->
statement.bind {
it.artistId.bind(1)
it.artistName.bind("New Artist")
}.execute()
}

Using values:

Artists.insert(Artists.artistId, Artists.artistName).use { statement ->
statement.values(2, "Another Artist").execute()
}

Using select:

// INSERT INTO Artists (artistName) SELECT name FROM TempArtists;
Artists.insert(Artists.artistName).use { statement ->
statement.select { TempArtists.select(TempArtists.name) }.execute()
}

Using executeReturning to get the inserted row's data (e.g., auto-generated ID):

// Assuming 'artistId' is an auto-incrementing primary key
val newArtistId = Artists.insert(Artists.artistName).use { statement ->
statement.values("New Artist").executeReturning(Artists.artistId)
}

Author

MOHAMMAD AZIM ANSARI

See also

Functions

Link copied to clipboard
abstract fun bind(binding: Bind.(T) -> Unit): InsertStatement<T>

Specifies the column-to-value mappings for the INSERT statement using a lambda.

Link copied to clipboard
abstract fun execute()

Executes the prepared INSERT statement.

Link copied to clipboard
abstract fun <T> executeReturning(column: KQLiteColumn<T>): T

Executes the INSERT statement and returns the value of a single column from the newly inserted row.

abstract fun executeReturning(vararg columns: KQLiteColumn<*>): KQLiteCursor

Executes the INSERT statement and returns a KQLiteCursor containing the data from the newly inserted row for the specified columns.

Link copied to clipboard
abstract fun select(selectStatement: () -> SelectStatement<*>): InsertStatement<T>

Specifies that the data for the INSERT statement will come from a SELECT query.

Link copied to clipboard
abstract fun values(vararg args: Any?): InsertStatement<T>

Specifies the values for the INSERT statement as a list of arguments.