我试图将数据集插入带有用户列表的Room Db中,但我无法将数据传递给实体Class。无论如何有插入数据,请帮帮我。谢谢。
var userDetails = User()
userDetails.username =“ Karthik”
我无法将“ Karthik”分配给userName。
最佳答案
Create a Dao for UserDetailDao `
@Dao
interface UserDetailDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(userDetails: User)`
//If you want to update or insert a lists then you can do as
@Transaction
suspend fun insertUserDetails( userDetails: List<User>) {
// delete your row on basis of primary key
// then iterate list and insert
userDetails.forEach {
insert(it)
}
}
}
然后在您的存储库类或视图模型中,您可以更新表
var userDetails = User()
userDetails.username = "Karthik"
然后使用
dao.insert(userDetails)