Android RoomDB自定义更新查询给出了android.database.sqlite.SQLiteConstraintException:NOT NULL约束失败:

时间:2018-08-19 12:10:00

标签: java android sqlite android-sqlite android-room

当我尝试使用两个String参数运行updateAlarmTriggered查询时,出现以下错误:

Logcat输出

E/SQLiteLog: (1299) abort at 33 in [UPDATE alarmTable SET triggered = 1 AND triggerTime = ? WHERE date = ?]: NOT NULL constraint failed: alarmTable.triggered

android.database.sqlite.SQLiteConstraintException: NOT NULL constraint failed: alarmTable.triggered (code 1299)

这是我的Dao和Entity类:

AlarmDao.java

@Query("UPDATE alarmTable SET triggered = 1 AND triggerTime = :triggerTime WHERE date = :alarmDate")
    void updateAlarmTriggered(String alarmDate, String triggerTime);

AlarmEntity.java

@PrimaryKey
@NonNull
private String date;

@NonNull
private int triggered;

@Nullable
private String triggerTime;

@NonNull
public String getDate() {return date; }

public void setDate(@NonNull String date) {this.date = date; }

@NonNull
public int getTriggered() {return triggered; }

public void setTriggered(@NonNull int triggered) { this.triggered =triggered; }

我通过以下方式初始化新实体:

@Insert
    void insertAlarm(AlarmEntity alarmEntity);

还有alarmEntity对象I:

 AlarmEntity newAlarm = new AlarmEntity();
 newAlarm.setDate(time.substring(4,20) + time.substring(30,34));
 newAlarm.setTriggered(0);
 newAlarm.setDeleted(0);

我无法弄清楚在警报表。触发的<空>约束上失败的原因,因为那是我在updateQuery上将其设置为1的原因。

1 个答案:

答案 0 :(得分:2)

要更新多个列,请使用逗号,来分隔列。喜欢

@Query("UPDATE alarmTable SET triggered = 1, triggerTime = :triggerTime WHERE date = :alarmDate")
void updateAlarmTriggered(String alarmDate, String triggerTime);