无法在 Room 数据库中更新布尔数组。

huangapple go评论78阅读模式
英文:

Cannot update Boolean Array in Room database

问题

我正在尝试在 Android Room 数据库中更新一个布尔数组,但是它抛出了以下错误:

E/SQLiteLog: (1) near "?": syntax error in "UPDATE alarm_table SET mDaysOfWeekArr=?,?,?,?,?,?,?,? WHERE mAlarmId=?"

查询代码(AlarmDao.java):

    @TypeConverters({Converter.class})
    @Query("UPDATE alarm_table SET mDaysOfWeekArr=:daysOfWeekArr WHERE mAlarmId=:alarmId")
    void updateRecAlarmStatus(Boolean[] daysOfWeekArr, int alarmId);

在插入/读取布尔数组时,正确的值保存在数据库中(使用查询)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insert(AlarmEntity alarmEntity);

Room 数据库存储库(AlarmRepository.java):

    public void updateRecAlarmStatus(final Boolean[] daysOfWeekArr, final int alarmId) {
        databaseWriteExecutor.execute(new Runnable() {
            @Override
            public void run() {
                alarmDao.updateRecAlarmStatus(daysOfWeekArr, alarmId);
            }
        });
    }

通过在 RecyclerView 中的点击事件调用上述函数,并使用 getter 从实体中读取数据并在 Boolean[] 中更改值:

daysOfWeek[1] = true;
daysOfWeek[5] = true;
ar.updateRecAlarmStatus(daysOfWeekArr, alarmId);

实体类:(AlarmEntity.java)

@Entity(tableName = "alarm_table")
public class AlarmEntity {

    // 用于禁用/启用/删除警报的 ID
    @PrimaryKey
    private int mAlarmId;

    // 警报的触发时间
    private long mAlarmTime;
    private boolean mAlarmEnabled;

    @TypeConverters({Converter.class})
    private Boolean[] mDaysOfWeekArr;

......
...... }

TypeConverter:

public class Converter {

    // 用于 AlarmEntity 的 Boolean[] mDaysOfWeek

    @TypeConverter
    public static Boolean[] fromString(String value) {
        Type listType = new TypeToken<Boolean[]>() {
        }.getType();
        Log.e("Converter: ", "fromString Called");
        return new Gson().fromJson(value, listType);
    }

    @TypeConverter
    public static String fromBoolean(Boolean[] list) {
        Gson gson = new Gson();
        Log.e("Converter: ", "fromString Called");
        return gson.toJson(list);
    }

}

这是我第一次在 StackOverflow 上提问... 如果需要更多的信息/代码,请询问。

英文:

I'm trying to update a Boolean array in android room database but it throws this error

E/SQLiteLog: (1) near &quot;?&quot;: syntax error in &quot;UPDATE alarm_table SET mDaysOfWeekArr=?,?,?,?,?,?,?,? WHERE mAlarmId=?&quot;

Query:(AlarmDao.java)

    @TypeConverters({Converter.class})
    @Query(&quot;UPDATE alarm_table SET mDaysOfWeekArr=:daysOfWeekArr WHERE mAlarmId=:alarmId&quot;)
    void updateRecAlarmStatus(Boolean[] daysOfWeekArr, int alarmId);

When Inserting / Reading the Boolean array correct values are saved in db (Using Query)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insert(AlarmEntity alarmEntity);

Room Db Repository(AlarmRepository.java)

    public void updateRecAlarmStatus(final Boolean[] daysOfWeekArr, final int alarmId) {
        databaseWriteExecutor.execute(new Runnable() {
            @Override
            public void run() {
                alarmDao.updateRecAlarmStatus(daysOfWeekArr, alarmId);
            }
        });
    }

Calling above function with onClick from recyclerView and reading data from entity using getters and changing values in Boolean[]

daysOfWeek[1] = true;
daysOfWeek[5] = true
ar.updateRecAlarmStatus(daysOfWeekArr, alarmId);

Entity Class: (AlarmEntity.java)

@Entity(tableName = &quot;alarm_table&quot;)
public class AlarmEntity {

    // ID used to disable / enable / delete alarms
    @PrimaryKey
    private int mAlarmId;

    // Trigger time for alarm
    private long mAlarmTime;
    private boolean mAlarmEnabled;

    @TypeConverters({Converter.class})
    private Boolean[] mDaysOfWeekArr;

......
...... }

TypeConverter

public class Converter {

    // Used by AlarmEntity Boolean[] mDaysOfWeek

    @TypeConverter
    public static Boolean[] fromString(String value) {
        Type listType = new TypeToken&lt;Boolean[]&gt;() {
        }.getType();
        Log.e(&quot;Converter: &quot;, &quot;fromString Called&quot;);
        return new Gson().fromJson(value, listType);
    }

    @TypeConverter
    public static String fromBoolean(Boolean[] list) {
        Gson gson = new Gson();
        Log.e(&quot;Converter: &quot;, &quot;fromString Called&quot;);
        return gson.toJson(list);
    }

}

This is my first time asking on stackoverflow.. Please ask if more info/code is required

EDIT:

After fetching one alarmEntity object from db and modifying its values
I'm able to save the changes using inbuilt @Update query

but this query does not work... I'm sure below
SET mDaysOfWeekArr=:daysOfWeekArr

is wrong but I don't know how to fix it

    @TypeConverters({Converter.class})
    @Query(&quot;UPDATE alarm_table SET mDaysOfWeekArr=:daysOfWeekArr WHERE mAlarmId=:alarmId&quot;)
    void updateRecAlarmStatus(Boolean[] daysOfWeekArr, int alarmId);

答案1

得分: 0

mDaysOfWeekArr 在 SQLite 中被存储为 TEXT,因此您不能使用 Boolean[] 执行类似于 UPDATE alarm_table SET mDaysOfWeekArr=:daysOfWeekArr WHERE mAlarmId=:alarmId 的操作,这就是您遇到该错误的原因。

我建议将您的 Dao 方法更改为 void updateRecAlarmStatus(String daysOfWeekArr, int alarmId),并在调用此方法之前进行 JSON 转换。

另外注意,在 Dao 方法带有 @Query 注解时,似乎 Room 无法使用 TypeConverters

英文:

The mDaysOfWeekArr is stored as TEXT in SQLite, so you can't do an UPDATE alarm_table SET mDaysOfWeekArr=:daysOfWeekArr WHERE mAlarmId=:alarmId with a Boolean[], hence you get that error.

I'd change your Dao method to be void updateRecAlarmStatus(String daysOfWeekArr, int alarmId), and do your JSON conversion before the calling of this method.

EDIT: Also it doesn't seem that Room is able to use TypeConverters when the Dao method has a @Query annotation.

huangapple
  • 本文由 发表于 2020年10月10日 20:44:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/64293577.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定