【发布时间】:2018-01-29 01:39:25
【问题描述】:
我正在为我的新项目使用 android 房间持久性库。
我想更新表格的某些字段。
我在Dao 中尝试过 -
// Method 1:
@Dao
public interface TourDao {
@Update
int updateTour(Tour tour);
}
但是当我尝试使用此方法进行更新时,它会更新与游览对象的主键值匹配的实体的每个字段。
我用过@Query
// Method 2:
@Query("UPDATE Tour SET endAddress = :end_address WHERE id = :tid")
int updateTour(long tid, String end_address);
它正在工作,但在我的情况下会有很多查询,因为我的实体中有很多字段。我想知道如何更新某些字段(不是全部),例如 Method 1 where id = 1; (id 是自动生成的主键)。
// Entity:
@Entity
public class Tour {
@PrimaryKey(autoGenerate = true)
public long id;
private String startAddress;
private String endAddress;
//constructor, getter and setter
}
【问题讨论】:
-
如何更新表中的列表。实际上我已经通过 TypeConverter 在表中插入了列表。但是,虽然带有更新,但它不起作用。请提出建议,如果您遇到任何此类问题。
-
@AmanGupta-ShOoTeR 您对上述评论有什么解决方案吗?
-
我的库 Kripton Persistence 库的工作方式与那个 Room 库非常相似。如果你想看看我是如何使用 Kripton 解决这个问题的,请访问abubusoft.com/wp/2019/10/02/…
-
@AmanGupta-ShOoTeR 我在使用“@Query”进行更新时遇到了此类问题。然后我通过创建具有相同主键值而不是更新的对象来使用“@Insert(onConflict = OnConflictStrategy.REPLACE)”并且它起作用了
标签: android sql-update android-room