如何将自定义查询段传递给RoomDb中的@query

时间:2018-09-11 13:47:44

标签: android sql android-room

我试图将Where子句传递给RoomDbD中的现有查询

@Query("SELECT products_table.id,products_table.name as productName,products_table.image,variations_table.name as variation,variations_table.id as variation_id,variations_table.default_sell_price as selling_price  from products_table INNER JOIN variations_table ON products_table.id = variations_table.product_id LEFT JOIN variations_locations_details_table as VLD ON variations_table.id = VLD.variation_id || :whereClause ")
LiveData<List<VariedProducts>> getProductsWithVariations(String whereClause);

它返回所有数据,而与任何查询无关,例如

productsDao.getProductsWithVariations("WHERE products_table.id = 4");

任何会议室DB用户都会提供帮助

2 个答案:

答案 0 :(得分:0)

您不会像在旧的android SQLite中那样传递where子句,而是传递要在where子句中使用的参数。

在Room中,您将编写整个查询,包括带有命名参数的where子句,例如PHP DBO或Java JDBC,然后在调用方法时传递参数。

@Query("SELECT * FROM user WHERE username LIKE :username LIMIT 1")
LiveData<User> findByUserName(String username);

因此,对于您而言,正确的方法是:

@Query("SELECT products_table.id,products_table.name as productName,products_table.image,variations_table.name as variation,variations_table.id as variation_id,variations_table.default_sell_price as selling_price  from products_table INNER JOIN variations_table ON products_table.id = variations_table.product_id LEFT JOIN variations_locations_details_table as VLD ON variations_table.id = VLD.variation_id WHERE products_table.id = :productId")
LiveData<List<VariedProducts>> getProductsWithVariations(Integer productId);

答案 1 :(得分:0)

您不能将列作为变量传递

只需使用多个查询