当我尝试使用两个字符串参数运行 updateAlarmTriggered Query 时,出现以下错误:
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);
我无法弄清楚为什么 NOT NULL 约束在 alarmTable.triggered 上失败,因为这就是我在 updateQuery 中将其设置为 1 的原因。
最佳答案
要更新多个列,请使用逗号 ,
来分隔列。喜欢关注
@Query("UPDATE alarmTable SET triggered = 1, triggerTime = :triggerTime WHERE date = :alarmDate")
void updateAlarmTriggered(String alarmDate, String triggerTime);
关于java - Android RoomDB 自定义更新查询给出 android.database.sqlite.SQLiteConstraintException : NOT NULL constraint failed:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51917516/