1

I have simple data object that I want to insert into room database but as I am using auto increment on my primary key I am doing it as below

@Dao
interface T1Dao {

   @Query("INSERT INTO tbl_t1(data1, data2) VALUES ( :T1.data1, :T1.data2) ")
   fun insert(note: T1): Long

}

I have many properties in T1 so I don't want pass them separately if possible. In above example I am just showing two properties.

sergiy tikhonov
  • 4,961
  • 1
  • 10
  • 27
Vishal Pawar
  • 4,324
  • 4
  • 28
  • 54

1 Answers1

1

But you can just use @Insert and not to set your primary key field, can't you?

@Insert
fun insert(note: T1): Long

Let's say you have T1 class:

@Entity
data class T1(
   @PrimaryKey(autoGenerate = true)
   val id: Int = 0, // This lets you not to set id before inserting 
   val data1: String,
   val data2: String
)

Then you can insert:

dao.insert(T1(data1 = "data1", data2 = "data2")) // just don't set id
sergiy tikhonov
  • 4,961
  • 1
  • 10
  • 27