[英]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.