Default Values and Inserts
If we @Insert
our entity, though, the values for all columns get specified via properties on that entity. After all, regardless of whether or not we provide text
or version
when we call the DefaultValueEntity
constructor, the resulting object will have values for those properties, just via the Kotlin defaults. So, our resulting INSERT
SQL statement will provide values for all of those properties, meaning that the SQL default values will not be used.
So, once again:
However, @Insert
is not the only option for executing INSERT
statements in Room. We can also do that using @Query
:
@Insert
fun insert(entity: DefaultValueEntity)
@Query("INSERT INTO defaultValue (id, title) VALUES (:id, :title)")
fun insertByQuery(id: String, title: String)
Here we have two DAO functions for inserting into our defaultValue
table. The first uses @Insert
. The second uses @Query
, with a SQL INSERT
statement that does not provide values for text
or version
. So, when we call insertByQuery()
, our resulting database table row will contain the supplied id
and title
values, plus the SQL default values for text
and version
.
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.