小编Ras*_*sel的帖子

在android Room中更新实体的某些特定字段

我正在为我的新项目使用android房间持久性库.我想更新一些表的字段.我试过像我一样Dao-

// Method 1:

@Dao
public interface TourDao {
    @Update
    int updateTour(Tour tour);
}
Run Code Online (Sandbox Code Playgroud)

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

// Method 2:

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

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

// Entity:

@Entity
public class Tour {
    @PrimaryKey(autoGenerate = true)
    public long id;
    private String startAddress;
    private String endAddress;
    //constructor, getter and setter
}
Run Code Online (Sandbox Code Playgroud)

android sql-update android-room

92
推荐指数
5
解决办法
7万
查看次数

标签 统计

android ×1

android-room ×1

sql-update ×1