有没有办法动态查询数据库 [英] Is there a way to dynamically query the database

查看:100
本文介绍了有没有办法动态查询数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们要为会议室数据库构建一个过滤器,用户可以在其中选择过滤器选项.

We want to build a filter for a room database, where the filter options are selected by the user.

即 我们有一个带有字段(id,名称,日期,类型)的实体 用户可以按日期之间的日期和/或名称包含文本和/或类型等于某项的内容过滤列表

i.e. we have an entity with field (id, name, date, type) the user may filter a list by date between and/or name contain text and/or type equals something

有没有办法在房间里做到这一点?

Is there a way to do this in room?

推荐答案

很抱歉,目前无法以您想要的方式实现.

I'm sorry to say that this is not currently possible in the way you want.

我已经设法通过使用db.query(query, values)方法来做到这一点.在运行时生成查询字符串(使用占位符'?'),并传递运行时生成的值数组.请注意,这将返回Cursor,而不是您想要的特定对象的实例,因此您将必须定义将Cursor映射到POJO的方法.

I have managed to do it by using the db.query(query, values) method. Generate your your query string at runtime (with placeholders '?'), and pass an array of runtime generated values. Note that this will return a Cursor, not an instance of the specific object you want, so you will have to define a method for mapping the Cursor to a POJO.

我已经将一些链接附加到了我的Cursor2Pojo Mapper和实现它的项目中.随时使用它,因为它应该以某种优美的方式解决您的问题.它支持列表返回和单实例返回,尽管需要在类中添加更多注释(Room注解是类绑定的,因此您无法在运行时通过反射获得它们)

I've attached some links to my Cursor2Pojo Mapper and an project implementing it. Feel free to use it, as it should solve your problem in a somewhatgraceful manner. It supports list and single instance returns, although requires you add more annotations to your class (Room annotations are class bound so you cannot get them through reflection at runtime)

Cursor2Pojo自定义库

第66-72行的项目实施

这篇关于有没有办法动态查询数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆