android - 如何将自定义查询段传递给 RoomDb 中的 @query
问题描述
我正在尝试将 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");
任何房间数据库用户协助
解决方案
您不会像在旧的 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);
推荐阅读
- firebase - Firebase 向经过身份验证的用户推送通知
- python - 不要用 cv.drawrectangle 绘制矩形?
- python - 如何绘制堆积条
- python - 运行逻辑回归代码时出现 Pyspark 错误
- linux - 使文件只读,除非通过符号链接路径访问
- php - 使用 HTML.PHP 而不是 html.twig - 不工作
- robotframework - 如何使用在项目级别创建的测试用例级别的变量和用户关键字
- mongodb - Mongo Map-Reduce 查询
- java - 即使字符串匹配,string contains() 方法也会返回 false
- python - 如何以python字符串格式传递整个列表?