会议室数据库发生冲突,无法正常工作,始终已插入数据

时间:2018-11-21 01:06:29

标签: android android-room android-architecture-components android-livedata

我试图从API提取一些数据并将其保存到房间数据库中,但是我有一个专栏,并且正在更改其值“ isFavourite”。

我在一些答案中看到使用upsert函数,并且尝试使用它,但每次仍覆盖数据。

@Dao
public abstract class ChannelDao {

@Insert(onConflict = OnConflictStrategy.IGNORE)
abstract long addChannelData(Channel channel);

@Query("UPDATE channel_table SET name= :name, url= :url , icon= :icon , streamer= :streamer , package_id= :package_id WHERE number= :number")
abstract void updateChannelData(String number, String name, String url, String icon, String streamer, String package_id);

@Transaction
void insOrUpdate(Channel channel) {
   long id = addChannelData(channel);
    if (id == -1) {
     updateChannelData(channel.getNumber(), channel.getName(), channel.getUrl(), channel.getIcon(), channel.getStreamer(), channel.getPackage_id());
}
}


@Entity(tableName = "channel_table", foreignKeys = @ForeignKey(entity = ChannelsPackage.class, parentColumns = "id", childColumns = "package_id", onDelete = CASCADE))

public class Channel {

@NonNull
@PrimaryKey
@SerializedName("number")
@Expose
private String number;
@SerializedName("name")
@Expose
private String name;
@SerializedName("url")
@Expose
private String url;
@SerializedName("icon")
@Expose
private String icon;
@SerializedName("streamer")
@Expose
private String streamer;

private boolean favorite;

private boolean locked;

private String package_id;

public Channel(@NonNull String number, String name, String url, String icon, String streamer, boolean favorite, boolean locked, String package_id) {
    this.number = number;
    this.name = name;
    this.url = url;
    this.icon = icon;
    this.streamer = streamer;
    this.favorite = favorite;
    this.locked = locked;
    this.package_id = package_id;
}
@NonNull
public String getNumber() {
    return number;
}

public void setNumber(@NonNull String number) {
    this.number = number;
}

public String getName() {
    return name;
}

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

public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}

public String getIcon() {
    return icon;
}

public void setIcon(String icon) {
    this.icon = icon;
}

public String getStreamer() {
    return streamer;
}

public void setStreamer(String streamer) {
    this.streamer = streamer;
}

public boolean isFavorite() {
    return favorite;
}

public void setFavorite(boolean favorite) {
    this.favorite = favorite;
}

public boolean isLocked() {
    return locked;
}

public void setLocked(boolean locked) {
    this.locked = locked;
}

public String getPackage_id() {
    return package_id;
}

public void setPackage_id(String package_id) {
    this.package_id = package_id;
}
}

2 个答案:

答案 0 :(得分:0)

更改为onConflictStrategy.ABORT。这样它将中止交易。

答案 1 :(得分:0)

搜索并搜索4天后,发现了问题, 我有2个表channel_table和package_table

它们之间的关系太多了,我每次在通道之前添加软件包,并且它们是“ onDelete = CASCADE” 这就是为什么我每次尝试更新时都会删除所有频道的原因。

对于要制作upsert方法的信息,将如下所示:

@Insert(onConflict = OnConflictStrategy.REPLACE)
    abstract void addChannelData(Channel channel);

 @Query("UPDATE channel_table SET name= :name, url= :url , icon= :icon , streamer= :streamer , package_id= :package_id WHERE number= :number")
    abstract void updateChannelData(String number, String name, String url, String icon, String streamer, String package_id);

@Transaction
    @Query("SELECT COUNT(*) from channel_table WHERE number = :number")
    abstract int isChannelExist(String number);

    @Transaction
    void insOrUpdate(List<Channel> channels) {
        for (Channel channel : channels) {
            if (isChannelExist(channel.getNumber()) > 0) {
                updateChannelData(channel.getNumber(), channel.getName(), channel.getUrl(), channel.getIcon(), channel.getStreamer(), channel.getPackage_id());
            } else {
                addChannelData(channel);
            }
        }

    }