当我尝试使用两个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);


我无法弄清楚为什么在AlarmTable.triggered上NOT NULL约束失败的原因,因为那是我在updateQuery上将其设置为1的原因。

最佳答案

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

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

09-15 21:36