i have a query like this
#Query("SELECT * FROM table WHERE type IN(:types) AND selection = 0 ORDER BY timestamp DESC")
my problem is that i have one type in my type list lets say it's type=4 for which i want selection = 1.
Is it possible to get the results in one query and how should the query look like?
I have a room database setup and I want to query that database N number of times and combine the results of each query into a live data array to display back to the user.
I'm pretty sure I want to be using MediatorLiveData but every example online has a predefined amount of live data sources it is combining.
I have the following setup:
petDao
#Query("SELECT * FROM pet_table WHERE name LIKE :petName")
fun getPetsByPetName(petName: String): LiveData<Pet>
petRepository
fun getPetsByPetName(petNames: List<String>): LiveData<List<Pet>> {
for (petName: String in petNames) {
val pets = petDao.getPetsByPetName(petName)
// Combine into one live list of pets
}
}
Have you tried this in your DAO?
#Query("SELECT * FROM pet_table WHERE name IN (:petNames)")
fun getPetsByPetName(petNames: List<String>): LiveData<List<Pet>>
It should work with a list of up to 999 arguments. (not sure if the parameter has to be an array, or if the list is fine)
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.
https://developer.android.com/reference/androidx/room/Query
To me it seems more appropriate for the example you've given.
I am loading a set of data from db using room and pagedList since it is a large set. I need to filter that data and show it to the user accordingly using the same pagedlistadapter. Please help.
dao is as follows
#WorkerThread
#Query("SELECT * FROM users WHERE name LIKE :query ORDER BY id DESC")
fun getAll(query: String): DataSource.Factory<Int, users>
Table has 4 fields - id,name,phone,address
You should use Transformations.switchMap() to requery the database any time the search term changes:
https://github.com/gavingt/upcoming-games/blob/cd7af7fbdd60933991ea7f8b966ca171bb594cb3/app/src/main/java/com/gavinsappcreations/upcominggames/ui/list/ListViewModel.kt#L35
Or see Google's Paging Library codelab, which does the same thing:
https://codelabs.developers.google.com/codelabs/android-paging/index.html?index=..%2F..%2Findex#1
#Query("SELECT * FROM users WHERE name LIKE :query ORDER BY id DESC")
fun getAll(query: String): DataSource.Factory<Int, users>
Here's your query string must be like %ABC% where abc is the name of the user.
The LIKE key word is use to check/find the pattern in the records Here you can read the possible pattern searches
To attach the % % to your query string you need to call this function as
YOUR_HELPER.getAll("%".plus(YOUR_QUERY).plus("%"))
plus() uses for Concatenation.
Is it possible to use SQLite's IN condition with Room?
I'm trying to select a list of items from my database where the value of a certain column (in this case a TEXT column) matches any one of a set of filter values. That's pretty easily done in SQL and SQLite, by my knowledge, just by adding an IN condition to your SELECT statement (see here). However, I can't seem to make it work with Room.
I keep getting this error:
Error:(70, 25) error: no viable alternative at input 'SELECT * FROM Table WHERE column IN :filterValues'
(where the input to the DAO #Query-annotated method is called filterValues)
I have tried three different methods now:
Passing the argument as a List<String>
Passing the argument as a String[]
And lastly passing the argument as simply a String, but formatted as (value_1, value_2, ..., value_n)
The last one in particular should work easily, as it will (or at least, it should) directly translate to SELECT * FROM Table WHERE column IN (value_1, value_2, ..., value_n), which is the exact way you would manually write out the SELECT if you were just accessing the database directly.
So as I was preparing to submit this, I double-checked a bunch of the stuff I had looked up previously and found the thing I had somehow missed and would have saved this question from being necessary.
As it turns out, both of these options:
Passing the argument as a List<String>
Passing the argument as a String[]
are viable (and you can replace String with any type the database can represent, such as char or int), you simply need to change the syntax in the #Query annotation from this:
#Query("SELECT * FROM Table WHERE column IN :filterValues")
to this:
#Query("SELECT * FROM Table WHERE column IN (:filterValues)")
Easy as pie, right?
Note that the third method above (passing the argument as simply a String, but formatted as (value_1, value_2, ..., value_n)) does not appear to be supported by Room, but that's probably not a bad thing, since that's the hard way.
Since I already had the whole thing typed out, I figured I would leave the question up in case other people are have as much difficulty finding this solution as I did and stumble upon this question.
Hi you can use this query:
#Query("SELECT * FROM user WHERE uid IN(:userIds)")
public abstract List findByIds(int[] userIds);
or
#Query("SELECT * FROM user WHERE uid IN(:userIds)")
public abstract List findByIds(List<Integer> userIds);
Similarly to above answers in Kotlin you can use vararg instead of array or list:
#Query("SELECT * FROM user WHERE uid IN (:userIds)")
fun getUsers(vararg userIds: Int): List<User>?
and use it like repository.getUsers(1, 2, 3).
If needed, to convert vararg to list see https://proandroiddev.com/kotlins-vararg-and-spread-operator-4200c07d65e1 (use *: format(output, *params)).
I am trying to implement a Room query which gets a list of integers that Room can use to return a list of entries based on that. Has anyone found a solution for that?
In the direction of that:
#Query("SELECT * FROM article_entries WHERE id IN :itemIds")
fun loadAll(itemIds : List<Int>): LiveData<List<Article>>
Replace:
#Query("SELECT * FROM article_entries WHERE id IN :itemIds")
with:
#Query("SELECT * FROM article_entries WHERE id IN (:itemIds)")