I have a code where I want to search the database with matches on the user's input using multiple like query.
code:-
String querysearch= "SELECT * FROM Data WHERE Value LIKE ? AND ObjectID LIKE ?"
cursor = sqldb.rawQuery(querysearch, new String[]{"%"+query + "%", " %"+query+"%"});
Based on what I've recently searched, I came up with that but unfortunately, it did not return any matches eventhough it has. Any help? Thanks!
Solved it. The problem is with me using the AND operator. If you want to strictly match your results, with the two conditions, use AND. But if you want whichever of your conditions to provide the match on your desired result, use OR.
Related
Making a query to realtime firebase
DatabaseReference mReference = mDatabase.getReference("Cards/" + language + "/" + gameTitle);
Query query = mReference.orderByChild("rank");
query.addValueEventListener
it suppose to sort it by rank and give as 1/2/3/4/5/6 values. Instead of that it returns 1/10/11/12/13/14/15/16/17/18/19/2/20/21 etc...
Thats how hierarchy looks like (sorry for russian language but dont have english yet)
Looks like it sorting by first character instead of whole value
it suppose to sort it by rank and give as 1/2/3/4/5/6 values. Instead of that it returns 1/10/11/12/13/14/15/16/17/18/19/2/20/21
That's the normal behaviour since you are ordering the results according to the rank property, which is of type String and not number. When you order strings, the elements are ordered lexicographically.
There are two ways in which you can solve this, either use my solution from the above answer or you change the type of your property to number. In my opinion, since all those value are numbers, is the solution you should go ahead with.
I need to search a list of keyword in Room. Currently i can search a single keyword like:
#Query("SELECT * FROM customer where content LIKE '%' || :keyword || '%' and status = 'published'")
LiveData<List<Customer>> getSearchResult(String keyword);
But how can i search a list of keyword in a easy way.
Is there easy way like IN with Collection arguments like:
#Query("SELECT first_name, last_name FROM user WHERE region IN (:regions)")
public List<NameTuple> loadUsersFromRegions(List<String> regions);
I know i can build query for multiple keyword manually but i want to pass a Collection as a argument.
Thanks in advance.
In SQL this is an easy thing to do by using FTS3 or FTS4 it's a Full Text Search these two are the newest features in SQL but I'm struggling on how to configure this into my android projects. I under stand a bit about room but not enough to give a an accurate answer I'm still researching this myself but have not found an answer. Please let me know if you find a way to configure it. I've been on this issue for weeks now but I know from knowing more about SQL that it is the FTS functions that are needed.
I have used below code for FTS search. Joined keyword with OR
String keywordAll = TextUtils.join("* OR *", keywordList);
keywordAll = "*" + keywordAll + "*";
For query used MATCH for search
WHERE Posts MATCH :keyword
Hope this will help others.
i'm trying to find a good way to sort the search results according to relevance after performing a search with a SearchView in Android. For me relevance means the number of matches in two SQLite text columns
I'm using a CursorLoader and there the sort order can be given to the constructor at the end
CursorLoader tLoader = new CursorLoader(
getActivity(), ContentProviderM.ARTICLE_CONTENT_URI,
tProj, tSel, tSelArgs, SORT_ORDER);
(or set using the setSortOrder (String sortOrder) method)
But i need more flexibility than this because i'm looking to sort on the number of matches rather than just on one or two columns
The only solution i can see myself is to add another column in my SQLite table, do some processing, and supply that column as the sort column to the CursorLoader
Now for my question: What is the best way to supply the sort order information to the CursorLoader using SQLite syntax, avoiding having to add a new column? (And what could this SQLite code look like?) Also, i'd like to ask more in general: Is there a different solution to this problem that i've missed?
Grateful for any help! And with kind regards,
Tord
Depending on the content provider, if it just pass to the orderBy field, you can do anything.
SQLiteDatabase query
orderBy How to order the rows, formatted as an SQL ORDER BY clause
(excluding the ORDER BY itself). Passing null will use the default
sort order, which may be unordered.
you can do whatever you want, this is just the line after ORDER BY
P.S. It is totally depending on the Content Provider, it it choose to ignore the parameter, you can do nothing.
i found a "workaround" for this problem.
After investigating different ways to write sqlite code i ended up just adding a new table column just for sorting. This column simply stores an integer and is updated every time that the user performs a search, right before the CursorLoader is created
Advantages:
We can now do all of the relevance calculations in Java code
Drawbacks:
Relevance calculation is done as the search is done so if we have a large number of items it may take some time to process everything
I am building an Android app that uses a SQLite database.
For this one task I have to run a query that looks like this:
SELECT item.id, item.price, t1.quantity
FROM item, (SELECT id, price
FROM list
WHERE list.state = 'sold') t1
WHERE item.id = t1.id
So far, I have tried:
Cursor c = resolver.query(uriRawQuery, null, selection, null, null)
where uriRawQuery is used to tell the ContentProvider that it should perform a db.rawQuery(selection, null) and selection is a string similar to the query above.
The problem is no data is returned into the Cursor. When I call c.moveToFirst() I get false.
The weird thing is that if I open the database file in SQLite Manager and run the exact same query I get results.
I know I can modify the query to make a join between the original list and item tables but I find it to be less efficient that way.
Any ideas would be very appreciated as I have spent too man hours on this already.
EDIT
I know what a join is, what I said is that it is a lot more efficient if I do it like this instead of using the entire list table.
I forgot a very important aspect
The WHERE clause looks like
" WHERE list.state = 'sold' and list.name like '" + arg + "%'"
where arg is a string.
I managed to solve the problem, I still don't know why this was happening but at least I got the Cursor to actually select the rows.
After many trials I thought about ditching the syntax above and write this instead:
" WHERE list.state = 'sold' and list.name like ? "
and move the argument in
selectionArgs = new String[]{arg + "%"}
I am going to wait a while before accepting the answer, in case someone provides an explanation as to why even though both queries look exactly the same they get different results.
I've got a fairly complicated query (multiple joins) on a normalized sqlite database. The query does a SELECT * to enable some automated attribute selection logic (so I can't eliminate the "*")
The problem I am having is that my result set contains multiple columns with the same attribute name. For example, one attribute common to each table in the query is "_id". When I go to call "cursor.getColumnIndex("_id")" the value returned is always the index of the last "_id" attribute in the result set column list (i.e. not the one I want). I'd love to be able to use my SQL alias prefixes like cursor.getColumnIndex("A._id") but that is not working.
QUESTIONs
It appears that cursor.getColumnIndex(AttributeName) returns the index of the last "AttributeName". Can anyone confirm this?
Also, any suggestions on how return the index of the 1st attribute with "AttributeName"? or better the Xth attribute having "AttributeName"?
You can do this:
SELECT _id as myID, * FROM myTable
This means the _id field will appear twice for each table in your results, but one of the two columns will have a unique name which should enable you to find it.
Unfortunately the documentation doesn't mention anything about what you need to do, so I am assuming it cannot be done.
However, you say
The query does a SELECT * to enable some automated attribute selection
logic (so I can't eliminate the "*")
What is this 'automated attribute selection logic' you speak of? Why do you require this?
An oder solution is:
"SELECT tableName.columnName FROM tableName"
and then do the same with:
cursor.getColumnIndex("tableName.columnName");
This is what MS-Access does. You can create a query and then see the generated SQL code (simply going to 'view' menu and selecting 'SQL view' from your query dessign window)