如何在查询中实现字符串数组?
How to implement a string array in a query?
我想通过以下查询从我的房间数据库中获取数据:
@Query("SELECT address.address_id, first_name, last_name, street, postal_code, city," +
"(SELECT employee.employee_id FROM employee WHERE employee.address_id = address.address_id) AS employee_id, " +
"(SELECT project.project_id FROM project WHERE project.address_id = address.address_id) AS project_id " +
"FROM address WHERE last_name IN (:pattern) OR first_name IN (:pattern) ORDER BY last_name ASC, first_name ASC")
LiveData<List<AddressBookAddress>> loadAddressBookWithFilter(String... pattern);
如您所见,我的参数接受一个包含一个或多个搜索字符串的数组。
让我们以 "max" 和 "muster"
为例
现在我想获取所有包含名字 "max" 或 "muster" 或姓氏 "max" 或 "muster" 的记录。但是目前,我根本没有收到任何数据。
文档显示它非常简单。看看Room Query docs。在那里你可以看到这个例子:
As an extension over SQLite bind arguments, Room supports binding a list of parameters to the query. At runtime, Room will build the correct query to have matching number of bind arguments depending on the number of items in the method parameter.
@Query("SELECT * FROM user WHERE uid IN(:userIds)")
public abstract List findByIds(int[] userIds);
For the example above, if the userIds is an array of 3 elements, Room will run the query as: SELECT * FROM user WHERE uid IN(?, ?, ?) and bind each item in the userIds array into the statement.
因此,将签名从 "String..." 更改为 "String[]" 以明确说明数组存在。
this page 上没有 kotlin 文档,但您可以使用此代码。
@Query("SELECT * FROM employee WHERE id IN(:idList)")
fun getAllSelectedEmployee(idList:Array<String?>): List<Model>
我想通过以下查询从我的房间数据库中获取数据:
@Query("SELECT address.address_id, first_name, last_name, street, postal_code, city," +
"(SELECT employee.employee_id FROM employee WHERE employee.address_id = address.address_id) AS employee_id, " +
"(SELECT project.project_id FROM project WHERE project.address_id = address.address_id) AS project_id " +
"FROM address WHERE last_name IN (:pattern) OR first_name IN (:pattern) ORDER BY last_name ASC, first_name ASC")
LiveData<List<AddressBookAddress>> loadAddressBookWithFilter(String... pattern);
如您所见,我的参数接受一个包含一个或多个搜索字符串的数组。
让我们以 "max" 和 "muster"
为例现在我想获取所有包含名字 "max" 或 "muster" 或姓氏 "max" 或 "muster" 的记录。但是目前,我根本没有收到任何数据。
文档显示它非常简单。看看Room Query docs。在那里你可以看到这个例子:
As an extension over SQLite bind arguments, Room supports binding a list of parameters to the query. At runtime, Room will build the correct query to have matching number of bind arguments depending on the number of items in the method parameter.
@Query("SELECT * FROM user WHERE uid IN(:userIds)") public abstract List findByIds(int[] userIds);
For the example above, if the userIds is an array of 3 elements, Room will run the query as: SELECT * FROM user WHERE uid IN(?, ?, ?) and bind each item in the userIds array into the statement.
因此,将签名从 "String..." 更改为 "String[]" 以明确说明数组存在。
this page 上没有 kotlin 文档,但您可以使用此代码。
@Query("SELECT * FROM employee WHERE id IN(:idList)")
fun getAllSelectedEmployee(idList:Array<String?>): List<Model>