TypeConverter not working when updating List in Room Database

后端 未结 2 540
终归单人心
终归单人心 2021-01-20 17:15

My query is showing a syntax error in the DAO_Impl build for some reason. I tried a rebuild but it still errors when conducting the following query:

Query:



        
相关标签:
2条回答
  • 2021-01-20 17:55

    Not too happy with this fix but I eventually served my purpose by creating a separate class that holds the value of the lists daysOfWeek and daysOfMonth:

    Interval:

    class Interval(_daysOfWeek: MutableList<Boolean> = mutableListOf(false, false, false, false, false, false, false),
                   _daysOfMonth: MutableList<Boolean> = mutableListOf(false, false, false, false, false, false, false, false, false,
                       false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false,
                       false, false, false, false, false, false)
    )
    {
        val daysOfWeek = _daysOfWeek
        val daysOfMonth = _daysOfMonth
    }
    

    and then just added a custom converter for this. Now I can query an update:

    @Query("UPDATE TasksTable SET name = :name, frequency = :frequency, interval = :interval, haveSchedule = :haveSchedule, schedule = :schedule, scheduleString = :scheduleString, description = :description, showProgressLayout = :showProgressLayout, intervals = :intervals WHERE taskID = :taskID")
    fun updateTask(taskID: Int, name: String, frequency: Int, interval: Int, haveSchedule: Boolean, schedule: Int, scheduleString: String, description: String, showProgressLayout: Boolean, intervals: Interval)
    
    0 讨论(0)
  • 2021-01-20 18:02

    So by the suggestion from Jeel Vankhede, I changed the MutableList to an ArrayList and the UPDATE query works. The difference in the Impl build :

    MutableList:

    @Override
      public void test(int tkID, List<Boolean> test) {
        StringBuilder _stringBuilder = StringUtil.newStringBuilder();
        _stringBuilder.append("UPDATE TasksTable SET test = ");
        final int _inputSize = test.size();
        StringUtil.appendPlaceholders(_stringBuilder, _inputSize);
        _stringBuilder.append(" WHERE taskID = ");
        _stringBuilder.append("?");
        final String _sql = _stringBuilder.toString();
        SupportSQLiteStatement _stmt = __db.compileStatement(_sql);
    

    ArrayList:

    @Override
      public void test(int tkID, ArrayList<Boolean> test) {
        final SupportSQLiteStatement _stmt = __preparedStmtOfTest.acquire();
        __db.beginTransaction();
        try {
          int _argIndex = 1;
          final String _tmp;
          _tmp = Converters.listBooleanToString(test);
          if (_tmp == null) {
            _stmt.bindNull(_argIndex);
          } else {
            _stmt.bindString(_argIndex, _tmp);
          }
          _argIndex = 2;
          _stmt.bindLong(_argIndex, tkID);
          _stmt.executeUpdateDelete();
          __db.setTransactionSuccessful();
        } finally {
          __db.endTransaction();
          __preparedStmtOfTest.release(_stmt);
        }
      }
    

    As you can see, the ArrayList uses the converter while the MutableList does not. Not sure why this is??

    0 讨论(0)
提交回复
热议问题