如何使用房间库更新整行,@ Update使用@Entity注释对象并通过引用主键更新它但是如何通过某些其他参数更新,其中某些值与某行中的单元格中的值匹配.
//Simple update
@Update
int updateObject(ObjectEntity... objectEntities);
//Custom Update
@Query(UPDATE TABLENAME ????)
int updateObject(ObjetEntity objectEntity,String field);
Run Code Online (Sandbox Code Playgroud)
我应该通过什么代替???? 这样新的objectEntity将被字段值匹配的旧object替换.
我有一个@Entity,它包含一个变量(自定义对象列表)以及该表的其他字段.我能够从这个实体插入,获取和删除.
但我在更新实体时面临一个问题:
我想更新该特定字段,该字段包含表中的自定义对象列表,但在编译时会抛出错误:
error: Query method parameters should either be a type that can be converted into a
database column or a List / Array that contains such type. You can consider adding a Type Adapter for this.
Run Code Online (Sandbox Code Playgroud)
我可以更新完整的行对象,但问题在于更新此单个字段.我在我的@Database类上使用TypeConverters但是我尝试在Dao和更新函数本身上使用它们但是它报告了相同的错误.
有人可以帮我更新行中的这个特定字段,我不想提供这个实体的完整对象来实现这一点.
我的实体是:
@Entity data class TableName(
@PrimaryKey
var id: String = "",
@SerializedName("varOne")
@Expose
var varOne: List<CustomObjects>? = null)
Run Code Online (Sandbox Code Playgroud)
更新方法是这样的:
@TypeConverters(MyTypeConverters.VarOneListTypeConverters::class)
@Query("Update TableName SET varOne = :varOneList")
abstract fun updateTableName(varOneList: List<CustomObjects>)
Run Code Online (Sandbox Code Playgroud) 我有以下实体:
@Entity
class Foo(
@PrimaryKey
@ColumnInfo(name = "id")
val id: Long,
@ColumnInfo(name = "thing1")
val thing1: String,
@ColumnInfo(name = "thing2")
val thing2: String,
@ColumnInfo(name = "thing3")
val thing3: String,
@ColumnInfo(name = "thing4")
val thing4: String
) {
@ColumnInfo(name = "local")
var local: String? = null
}
Run Code Online (Sandbox Code Playgroud)
其中本地是不存储在服务器上的信息,只存储在手机本地。
目前,当我从服务器中提取信息时,GSON 会自动填充我的值,但由于“本地”不是来自服务器,因此不会填充到该对象中。
有没有一种方法,当我调用 update 时,我可以让 Room 跳过“本地”列的更新,而无需编写自定义更新以插入除“本地”以外的所有其他列?痛点是我可以有很多列,我添加的每个新列都必须添加到自定义插入语句中。
我还考虑过从服务器实体到新的“本地”实体的一对一映射,但是现在我必须处理连接语句的痛苦,因为我需要本地信息。
我希望我能做这样的事情:
@Entity
class Foo(
@PrimaryKey
@ColumnInfo(name = "id")
val id: Long,
@ColumnInfo(name = "thing1")
val instructions: String,
@ColumnInfo(name = "thing2")
val instructions: String,
@ColumnInfo(name = …Run Code Online (Sandbox Code Playgroud)