Insert Statement
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
Executes the INSERT statement and returns the value of a single column from the newly inserted row.
Executes the INSERT statement and returns a KQLiteCursor containing the data from the newly inserted row for the specified columns.
Specifies that the data for the INSERT statement will come from a SELECT query.
Specifies the values for the INSERT statement as a list of arguments.