insert

fun <T : KQLiteTable> T.insert(vararg columns: KQLiteColumn<*>, onConflict: Action? = null, connection: SQLiteConnection? = null): InsertStatement<T>

Creates an InsertStatement to construct and execute an SQL INSERT query.

This function is the entry point for building more complex INSERT statements, such as those inserting specific columns, using VALUES clauses, or inserting from a SELECT statement. The returned InsertStatement must be configured further (e.g., by calling InsertStatement.bind or InsertStatement.values) and then executed.

The InsertStatement is AutoCloseable and should be managed using a use block to ensure underlying resources are properly released.

Example: Inserting specific columns with InsertStatement.values

// INSERT INTO Artists (artistName) VALUES ('New Artist');
Artists.insert(Artists.artistName).use {
it.values("New Artist").execute()
}

Example: Inserting using InsertStatement.bind block

// INSERT INTO Artists (artistId, artistName) VALUES (10, 'Another Artist');
Artists.insert(Artists.artistId, Artists.artistName).use { statement ->
statement.bind {
it.artistId.bind(10)
it.artistName.bind("Another Artist")
}.execute()
}

Example: Inserting from a SELECT statement

// INSERT INTO Artists (artistName) SELECT name FROM TempArtists WHERE country = 'CA';
Artists.insert(Artists.artistName).use {
it.select {
TempArtists
.select(TempArtists.name)
.where { TempArtists.country EQ "CA" }
}.execute()
}

Return

An InsertStatement to construct and execute the INSERT query.

Author

MOHAMMAD AZIM ANSARI

Parameters

columns

A list of columns to be inserted into the table.

onConflict

Optional conflict resolution strategy (e.g., Action.IGNORE, Action.REPLACE). If null, the default database conflict behavior is used.

connection

Optional SQLiteConnection to use for executing the statement. If null, the default connection is used.

Type Parameters

T

The type of the KQLiteTable.

See also