0

I have 5 columns in the database, 4 of which I fill out a request from the network. I'm trying to add data to the database specifically in these 4 columns via @QUERY, because when trying to do this via @Insert (onConflict = OnConflictStrategy.REPLACE), the value in the 5th column is set to default.

I would like to insert the entire list into the column through sqlite, that is, into the "id" List <Int>, in the "first_name" List <String>, etc. ? Is this possible, if so, what would the correct syntax look like? At the moment I am filling in the data through a for loop.

Here is the request:

@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
    )

1 Answer 1

1

For correct working out "@Insert (onConflict = OnConflictStrategy.REPLACE)" it is required to mark id with annotation "@PrimaryKey"

You entity model.

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

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

Link for primary key

Link for entity

Sign up to request clarification or add additional context in comments.

1 Comment

id was originally annotated with @PrimaryKey.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.