如何通过 SQLite kotlin 查询将列表添加到列 android

How to add list to column by SQLite kotlin query android

我在数据库中有 5 列,其中 4 列是我从网络上填写的请求。我正在尝试通过 @QUERY 在这 4 列中将数据添加到数据库中,因为当尝试通过 @Insert (onConflict = OnConflictStrategy.REPLACE) 执行此操作时,第 5 列中的值设置为默认值。

我想通过 sqlite 将整个列表插入到列中,即插入 "id" List <Int>"first_name" List <String> 等?这可能吗?如果可能的话,正确的语法是什么样的?目前我正在通过for循环填充数据。

请求如下:

@Query("INSERT INTO friends_table (id, first_name, last_name, photo) VALUES (:id, :firstName, :lastName, :photo)")

suspend fun insertAll(
        id: Int,
        firstName: String,
        lastName: String,
        photo: String
    )

为了正确计算“@Insert (onConflict = OnConflictStrategy.REPLACE)”,需要使用注解“@PrimaryKey”标记 id

你是实体模型。

@Entity
class EmployeeEntity(
        @PrimaryKey
        id: Int,
        firstName: String,
        lastName: String,
        photo: String
)

@Insert (onConflict = OnConflictStrategy.REPLACE)
suspend fun insertAll(entities: List<EmployeeEntity>)

Link 对于 primary key

Link 对于 entity