Updating with a parameter using a persistent library

How can I update the entire row using the room library, @Update will accept the annotated @Entity object and update it by reference to the primary key, but how will I update through some other parameter, for example update, where the specific value corresponds to the value in the cell in line.

//Simple update
@Update
int updateObject(ObjectEntity... objectEntities);

//Custom Update
@Query(UPDATE TABLENAME ????)
int updateObject(ObjetEntity objectEntity,String field);

What should I pass instead of ???? so the new objectEntity is replaced by the old, where the field value matches.

+9
source share
4 answers

, , Room . , Person :

@Entity(tableName = "people")
public final class Person {

    @PrimaryKey
    @ColumnInfo(name = "uuid")
    public final String uuid;

    @ColumnInfo(name = "name")
    public final String name;

    @ColumnInfo(name = "is_alive")
    public final Boolean isAlive;

    public Person(String uuid, String name, Boolean isAlive) {
        this.uuid = uuid;
        this.name = name;
        this.isAlive = isAlive;
    }
}

is_alive name. :

@Query("UPDATE people SET is_alive= :alive WHERE name = :name")
public abstract int setIsAliveByName(String name, int alive);

, , , , , UPDATE.

SELECT , db.

, ORM - ...

+15

, .

@Query("UPDATE DailyConsumption SET quantity = :quantity ,date_time= :dateTime,date= :date WHERE id LIKE :id ")
int updateItem(int id,int quantity,long dateTime,String date);
+6

@Query("UPDATE RecentDestinations SET readStatus=:readStatus WHERE name = :name AND street = :street AND state = :state AND postCode = :postcode")
void updateStatus(boolean readStatus,String name,String street,String suburb,String state,String postcode);
+1

There is no custom refresh function in the room to update data for multiple columns. But you can use some logic like.

Suppose you need to update last_name

@Entity(tableName = "user")
public class User {
  @NonNull
    @PrimaryKey
    private int id;

    private String user_id;

    private String first_name;

    private String last_name;

    private String email;
}

First you get an ObjectModel using a select query.

@Query("SELECT * FROM user WHERE email = :email")
User getUser(String email);

then get the id from this ObjectModel and set this id for your new ObjectModel

then update simply as a room request

@Update
void updateUser(User user);
0
source

All Articles