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)")
Related
I need to perform search operations in my app so I have tried to implement a Full-text search using a room in android. But I'm not getting the desired output.The query is not clear to me. I have mentioned the query below:
#Query("SELECT posts.postId,posts.authorName,posts.authorImageUrl,posts.title,posts.message,posts.thumbnailUrl,posts.downloadUrl,posts.postTimeStamp,posts.type,posts.seenByUser,posts.mimeType,posts.isBookmarked FROM postsFts JOIN posts ON (posts.postId = postsFts.postId)")
fun searchInPostsLocalDB(query: String): List<PostsModel>
I want to get lists of matching rows in output.
You are missing the match which checks which row is matching with the query. So the answer is:
#Query("SELECT * FROM postsFts JOIN posts ON (posts.postId = postsFts.postId) WHERE postsFts MATCH :query")
fun searchInPostsLocalDB(query: String): List<PostsModel>
And use * to surround the query to make it a wildcard.
For example : *tarun*
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.
I am new to Android studio, especially in room database.
I can't find out how to read a row from room database
//in dao
#Query("SELECT * from qarindosh_table LIMIT 1")
Qarindosh getAnyQarindosh();
I couldn't find out how to read a row. Please help.
Room database is working.
This is how you do it
#Query("SELECT * FROM qarindosh_table")
List<ModelClass> getAllItems();
If I understand you correctly, you wish to read 1 specific row from a database. In order to do that, you need to query by primary key, usually named id in the Entity. Something like that:
#Query("SELECT * FROM qarindosh_table WHERE id=(:id)")
Qarindosh loadQarindoshById(int id);
In my repository class, I want to fetch data only if none exists in a Room table. How to check whether any row exists in a table?
Use EXISTS operator, and return 1 means true and 0 means false.
If you want to check some specific row and some condition, do this trick:
#Query("SELECT EXISTS(SELECT * FROM tableName WHERE id = :id)")
fun isRowIsExist(id : Int) : Boolean
Or simple use this:
#Query("SELECT EXISTS(SELECT * FROM tableName)")
fun isExists(): Boolean
You can use EXISTS operator and return just Boolean:
#Query("SELECT EXISTS(SELECT * FROM table)")
fun hasItem(): Boolean
As ADM suggested, you might get lucky using COUNT() to count the actual rows in a table.
However, I would recommend just fetching the data anyway - if none exists Room will simply return an empty list, and this should not be less efficient than asking for the row count (if it's 0 anyway).
As a plus you will have less code to write to get the functionality you want! :-)
I have 2 queryes from ROOM DB:
#Query("SELECT * FROM basket_items")
Flowable<List<BasketItem>> getAll();
#Query("SELECT SUM(count*amount) FROM basket_items")
Flowable<Long> totalSum();
How can I merge this queryes to one query? And what type of model it will be return?
Room Queries can return POJO (not an #Entity), and therefore you can make temporary model for your DAO.
As I already asked in comments, I don't know what you're trying to achieve (as in what value you want to get from those queries).
But hopefully this example might help:
YourDao.java
#Query("SELECT name AS nameItem, (count * amount) AS countItem FROM basket_items")
Flowable<List<CountedBasketItem>> getAllCounted();
static class CountedBasketItem {
String nameItem;
Long countItem;
}
Additional Answer
What you want to return List and total sum is not possible by the query. SUM is aggregation method, you aggregate the list.
So there're 2 options that you might want to do:
Use GROUP BY in your query
Or execute those 2 Flowable and get the result.
you can create field totalSum in your pojo and use this query
#Query("SELECT *,SUM(count*amount) AS totalSum FROM basket_items")
Flowable<List<BasketItem>> getAll();
you will get all table and the field totalSum is SUM(count*amount)