+-
在android Room中更新实体的某些特定字段
我正在为我的新项目使用 android房间持久性库.
我想更新一些表的字段.
我试过像我的道 –

// Method 1:

@Dao
public interface TourDao {
    @Update
    int updateTour(Tour tour);
}

但是当我尝试使用此方法进行更新时,它会更新实体中与tour对象的主键值匹配的每个字段.
我用过@Query

// Method 2:

@Query("UPDATE Tour SET endAddress = :end_address WHERE id = :tid")
int updateTour(long tid, String end_address);

它正在工作,但在我的案例中会有很多查询,因为我的实体中有很多字段.我想知道如何更新某些字段(不是全部),如方法1,其中id = 1; (id是自动生成主键).

// 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

使用@Query,就像在方法2中一样.

is too long query in my case because I have many field in my entity

然后有更小的实体.或者,不要单独更新字段,而是与数据库进行更粗粒度的交互.

IOW,房间本身没有什么可以做你想要的.

点击查看更多相关文章

转载注明原文:在android Room中更新实体的某些特定字段 - 乐贴网