我一直在 Android Studio 上開發預算支出跟蹤應用程式。在這個特定部分中,我想要做的是將表“budget_tracker_table”的選定結果插入另一個表“budget_tracker_table_alias”。當我在應用程式檢查中執行它時,我撰寫的 SQL 沒有任何問題,當我將完全相同的行放在 DAO 類中時,它顯示錯誤,說“找不到方法‘值’”。這個問題有什么可能的解決方案嗎?謝謝。
@Insert("insert into budget_tracker_table_alias (date_alias, store_name_alias, product_name_alias, product_type_alias, price_alias, product_type_percentage) select date, store_name, product_name, product_type, price, count(*) * 100.0 / (select count(*) from budget_tracker_table where date >= '2022-4-22' and date <= '2022-4-23') as 'Percentage' from budget_tracker_table where date >= '2022-4-22' and date <= '2022-4-23' group by store_name;")
void insert(BudgetTrackerAlias budgetTrackerAlias);
uj5u.com熱心網友回復:
Room's@Insert是一個方便的插入,它只需要一個物件(或物件串列),并且該物件是相應物體的型別/類的物件。
您需要使用 @Query 和 SQL 來插入,所以
@Query("insert into budget_tracker_table_alias (date_alias, store_name_alias, product_name_alias, product_type_alias, price_alias, product_type_percentage) select date, store_name, product_name, product_type, price, count(*) * 100.0 / (select count(*) from budget_tracker_table where date >= '2022-4-22' and date <= '2022-4-23') as 'Percentage' from budget_tracker_table where date >= '2022-4-22' and date <= '2022-4-23' group by store_name;")
void insert();
當然,這可能用途有限,因為如果您想通過日期,日期是硬編碼的,那么您可以:-
@Query("insert into budget_tracker_table_alias (date_alias, store_name_alias, product_name_alias, product_type_alias, price_alias, product_type_percentage) select date, store_name, product_name, product_type, price, count(*) * 100.0 / (select count(*) from budget_tracker_table where date >= :fromDate and date <= :toDate) as 'Percentage' from budget_tracker_table where date >= :formDate and date <= :toDate group by store_name;")
void insert(String fromDate,String toDate);
PS 與@Deleteand相同@Update,即它們期望物件,但您可以使用@QueryUPDATE/DELETE SQL
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/476324.html
