I am trying to search a substring using room for example in string "I am eating mutton", I am trying to search "eating" as a substring, I am trying this query, it works fine if I search for exact substring "eating" but doesn't work if I search with "I am eating rice" etc.
#Query("select msg_temp from auto_reply where keyword LIKE '%'||:keyword||'%'")
fun selectKeyword(keyword: String): String
I have seen many answers, all are suggesting same method, even this query works in sqlite browser. Any help should be appreciated
Try using :-
#Query("select msg_temp from auto_reply where keyword LIKE :keyword")
fun selectKeyword(keyword: String): String
and then use
selectKeyword("%eating%")
an alternative (but case sensitive) :-
#Query("select msg_temp from auto_reply where instr(keyword,:keyword)")
wild characters are not considered so just selectKeyword("eating")
another that should work but!!!!
#Query("SELECT * FROM user WHERE name LIKE :wildchar || :name || :wildchar")
fun selectKeyword("eating","%")
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*
#Query("update bookmarks set bookmarks_path= replace(bookmarks_path,:oldPath,:newPath) where bookmarks_path like :oldPath || '%'")
fun updateBookmarksPath(oldPath: String, newPath: String): Completable
I am using the room database and this is my code
I found that the format of this code is not correct when running, android studio prompts me: expression expected, got'replace'
like this
I want to achieve the effect like this
----path------------------path
/root/1/1--------------/stack/1/1
/root/name/1------/stack/name/1
/root/cls/1-----------/stack/cls/1
/root/go/1-----------/stack/go/1
/root/js/1------------/stack/js/1
please
REPLACE is both an SQLite keyword and, in the case of your intended use, the name of a string function: replace() .
The Room annotation processor appears to be rejecting your query string because it is treating replace as keyword instead of function name. Maybe a bug?
Some quick experiments indicate that a possible solution is to surround replace in backticks to mark is as a name (see keyword link above). Please give it a try:
#Query("update bookmarks set bookmarks_path= `replace`(bookmarks_path,:oldPath,:newPath) where bookmarks_path like :oldPath || '%'")
fun updateBookmarksPath(oldPath: String, newPath: String): Completable
What would be the android room query to match a column data exactly match or start with the search string
VVV this seems to giving me only the exact match
select * from table where dna_sequence like :searchTerm
Thanks
this seems to giving me only the exact match
select * from table where dna_sequence like :searchTerm
There is nothing wrong with your query. The problem is with your Java or Kotlin code. You are getting an exact match because you are passing an exact to searchTerm and not using any wildcards. The LIKE operator in SQL allows us to use wildcards in the string to match more than just strict equality. _ will match a single character while % will match zero or more characters. If you don't use these wildcards, then LIKE will be the exact same as =. So you need to pass a value for searchTerm that uses one of these wild card characters. For example, if you a DAO interface declared as
#Dao
public interface MyDao {
#Query("SELECT * FROM foo WHERE search like :searchTerm")
public Foo[] getFooSearch(String searchTerm);
}
You can call the method with something like:
MyDao dao;
dao.getFooSearch("abcdef%");
which will match the start of the string.
Note: This example is a bit contrived because you only provided your SQL query and didn't provide any of the Java or Kotlin code for your DAO or Data Entity.
Try this in search method:
public String getResultForSearchString(String searchTerm){
String searchExptression=searchTerm+"%";
//.. perform search operation on SQLite with searchExpression
return result;
}
For more information visit: https://www.sqlitetutorial.net/sqlite-like/
Hope it helps.
Why does this not work in Room?:
val dataSourceFactory =
database.gameDao.getGames("Game.platforms LIKE '%XONE%'")
#Query("SELECT * FROM Game WHERE :likeClause")
fun getGames(likeClause: String): DataSource.Factory<Int, Game>
But this does?:
#Query("SELECT * FROM Game WHERE Game.platforms LIKE '%XONE%'")
fun getGames(): DataSource.Factory<Int, Game>
Is there any way to pass in a string that can stand in as part of the query?
EDIT: I know this isn't the correct way to form a single LIKE clause, but I'm actually trying to pass in multiple LIKE clauses. So I want a way to inject text directly into the query, but Room doesn't seem to want me to do that.
You are talking about dynamic SQL and I dont think this is possible with room. what would work is
#Query("SELECT * FROM Game WHERE Game.platforms LIKE :likeClause1 AND Game.publisher LIKE :likeClause2")
fun getGames(likeClause1: String, likeClause2: String): DataSource.Factory<Int, Game>
and you can use either AND or OR as needed and if you want to ignore one of the like clauses just pass an empty string
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)).