【问题标题】:Android Room Database Table is not updatingAndroid 房间数据库表未更新
【发布时间】:2018-09-08 10:25:12
【问题描述】:

我正在尝试使用 RX2 和房间更新我的表中的值。

我的物品是

@Entity(tableName = "myCustomTable")
public class MyItem  {

    @NonNull
    @PrimaryKey
    @ColumnInfo(name = "item_id")
    @SerializedName("item_id")
    private Long item_id;

    @ColumnInfo(name = "name")
    @SerializedName("name")
    private String name;

    @ColumnInfo(name = "country")
    @SerializedName("country")
    private String value;

    @SerializedName("image")
    @ColumnInfo(name = "image")
    private String image;

    @ColumnInfo(name = "latitude")
    @SerializedName("latitude")
    private Double latitude;

    @ColumnInfo(name = "longitude")
    @SerializedName("longitude")
    private Double longitude;

    @ColumnInfo(name = "valid")
    private boolean valid;

    public MyItem (Long item_id, String name, String value, String image, Double latitude, Double longitude, boolean valid) {
        this.item_id = item_id;
        this.name = name;
        this.value = value;
        this.image = image;
        this.latitude = latitude;
        this.longitude = longitude;
        this.valid = true;
    }

    public boolean isValid() {
        return valid;
    }

    public Long getItem_id() {
        return item_id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public Double getLatitude() {
        return latitude;
    }

    public Double getLongitude() {
        return longitude;
    }
}

我的查询是这样的:

@Dao
public interface CustomDao {
  @Insert(onConflict = OnConflictStrategy.REPLACE)
  void insertItem(MyItem item);

  @Query("UPDATE myCustomTable SET valid = :isValid WHERE item_id= :id")
  void flagQueued(long id, boolean isValid);
}

默认情况下valid为真;

这将被数据库实现调用并转换为可完成项

public class LocalDataSource implements DataSource {
  private CustomDao customDao;

  @Inject
  public LocalPupilDataSource(CustomDao customDao) {
    this.customDao = customDao;
  }

  public Completable flagQueued(long id) {
    return Completable.fromAction(() -> customDao.flagQueued(id,false));
  }
}

最后,我由订阅者调用 flagQueued 方法。此订阅者正在响应成功完成。

mCompositeDisposable.add(viewModel.deleteItem(id)
  .subscribeOn(schedulerProvider.computation())
  .observeOn(schedulerProvider.ui())
  .subscribe(() -> Snackbar.make(getActivity().findViewById(R.id.main_layout),
    R.string.user_removed, Snackbar.LENGTH_SHORT).show(), error -> Timber.e(error,error.getMessage()) ) );

很遗憾,该值未更新。当然,ID 存在于表中。

任何建议将不胜感激。

【问题讨论】:

  • 结果是 1 @pskink
  • item_id 长吗?也许您在查询中将 long 与不同的数据类型进行比较?请发布 MyItem 类
  • 嗨@NicolaGallazzi。刚刚用 Myitem 类更新了问题
  • 看起来不错。您可以使用此工具sqlitebrowser.org 查看数据库内部。在 Android Studio 中使用设备文件资源管理器并查看 data/data/yourapppackageid 以找到数据库
  • 我认为表名@Entity(tableName = "myCustomTable ") 中有一个空格。你能检查一下吗?

标签: android rx-java2 android-room


【解决方案1】:

发现问题。经过数小时的调试。

由于某种原因,我在构造函数this.valid = true; 上分配的默认值破坏了查询。

所以解决方法就是去掉错误值,当我将要插入到表中时,将我想要的值分配给Item。

是否有任何 GitHub 存储库可供我发布问题?

【讨论】:

  • 这个解决方案对我不起作用,插入工作正常,但更新不起作用 idk Y 这是一个可怕的错误.. 甚至不会抛出任何错误
猜你喜欢
  • 1970-01-01
  • 2019-01-17
  • 1970-01-01
  • 2022-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多