简体   繁体   English

如何通过将另外两列相乘来更新 android 房间数据库中的列值?

[英]How to update a column value in android room database by the multiplication of another two columns?

I have a column (A) and I want it is value to be the multiplication of column (B) by column (C).我有一列 (A),我希望它的值是列 (B) 与列 (C) 的乘积。

How to implement this in android room database?如何在 android 房间数据库中实现这个? What do I need to put into the Dao, the repository, the viewmodel and activity to make it work?我需要在 Dao、存储库、视图模型和活动中放入什么才能使其正常工作?

Done by the following code:通过以下代码完成:

first add the following code in the Dao:首先在Dao中添加如下代码:

 @Query ("UPDATE table SET A = B*C")
    void multiply();

then add this AcyncTask to the repository:然后将此 AcyncTask 添加到存储库:

private static class MultiplyAsyncTask extends AsyncTask<Void, Void, Void> {

        private SomeDao asyncTaskDao;

        MultiplyAsyncTask(SomeDao dao) {
            asyncTaskDao = dao;
        }

        @Override
        protected Void doInBackground(Void... voids) {
            asyncTaskDao.multiply();
            return null;
        }
    }

. . . . . . and this code also should be implemented in the repository:并且此代码也应该在存储库中实现:

public void multiplyColumns(){
        MultiplyAsyncTask task = new MultiplyAsyncTask (SomeDao);
        task.execute();
    }

Then call the method in the ViewModel:然后调用ViewModel中的方法:

public void multiplyColumns(){
        repository.multiplyColumns();
    }

Finally, implement this method in the Activity/Fragment where the data are inserted in the table in the onActivityResult:最后,在onActivityResult中的表中插入数据的Activity/Fragment中实现这个方法:

  SomeViewModel.insertData(data);
  SomeViewModel.multiplyColumns();

This worked with me, but please if you have better solution share it here.这对我有用,但是如果您有更好的解决方案,请在此处分享。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在同一个实体类中返回两个不同的数据库列,作为Android Room中的单个列? - How can I return two different database columns, within the same entity class, as a single column with Android Room? Android房间数据库更新方法 - How to update Room database in Android 如何使用单个查询更新所有行中的列 - Android Room Database - How to update a Column in all rows with single query - Android Room Database 连接 android 房间数据库中的两个字符串列 - concat two string columns in android room database Android 房间数据库如何按房间中的字符串值过滤 - Android Room Database How to Filter By String Value in Room 如何通过android room更新数据库设置新值到旧值? - How to update database set new value upon to old value by android room? Android从两个表中的两个列中求和数量值,并使用求和值更新其中一个列中的数量值 - Android Sum Quantity values from two Columns in two tables and update the Quantity value in one of the Column with the summed value 带有两张表的 Android 房间数据库 - Android room database with two tables 如何使用 android 房间的 where 条件更新数据库中的记录? - how to Update records in database using where condition in android room? 如何在下一版本的房间数据库Android中删除列? - How to delete column in next version of room database Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM