I am using android room persistence library for my new project.
I want to update some field of table.
I have tried like in my Dao
-
// Method 1:
@Dao
public interface TourDao {
@Update
int updateTour(Tour tour);
}
But when I try to update using this method then it updates every field of the entity where it matches primary key value of tour object.
I have used @Query
// Method 2:
@Query("UPDATE Tour SET endAddress = :end_address WHERE id = :tid")
int updateTour(long tid, String end_address);
It is working but there will be many queries in my case because I have many fields in my entity. I want to know how can I update some field (not all) like Method 1
where id = 1; (id is the auto generate primary key).
// Entity:
@Entity
public class Tour {
@PrimaryKey(autoGenerate = true)
public long id;
private String startAddress;
private String endAddress;
//constructor, getter and setter
}
I want to know how can I update some field(not all) like method 1 where id = 1
Use @Query
, as you did in Method 2.
is too long query in my case because I have many field in my entity
Then have smaller entities. Or, do not update fields individually, but instead have more coarse-grained interactions with the database.
IOW, there is nothing in Room itself that will do what you seek.
UPDATE 2020-09-15: Room now has partial entity support, which can help with this scenario. See this answer for more.