Is that possible to combine the results of 2 Cursors by using INNER JOIN approach (merge horizontally) and as a result have only one Cursor? I know CursorJoiner class, but couldn't understand how to combine the results into one Cursor with this.
EDIT:
Just as an information for the people who would need to do something like this. From my side I have tried to implement AbstractCursor. I have found this POST which is similar question answered by #CommonsWare.
I don't have enough rights, but someone else could mark the question as duplicate.
The cursor will store the query data,if you need something like an Inner Join, do it when create the query, then the cursor will have the result you want.
This question have an answer with rawQuery.
Related
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 would like to use SQLiteStatement in my ContentProvider instead of the rawQuery or one of the other standard methods. I think using SQLiteStatement would give a more natural, native, efficient and less error prone approach to doing queries.
The problem is that I don't see a way to generate and return a Cursor. I realize I can use "call" and return a Bundle, but that approach requires that I cache and return all selected rows at the same time - this could be huge.
I will start looking at Android source code - I presume that "query" ultimately uses SQLiteStatement and somehow generates a Cursor. However, if anyone has any pointers or knowledge of this, I would greatly appreciate your sharing.
I would like to use SQLiteStatement in my ContentProvider instead of the rawQuery or one of the other standard methods. I think using SQLiteStatement would give a more natural, native, efficient and less error prone approach to doing queries.
Quoting the documentation for SQLiteStatement:
The statement cannot return multiple rows or columns, but single value (1 x 1) result sets are supported.
I fail to see why you would bother with a ContentProvider for single row, single column results, but, hey, it's your app...
The problem is that I don't see a way to generate and return a Cursor
Create a MatrixCursor and fill in the single result.
I meet a situation that I can workaround but is seeking a better way to do. It is:
I have 8,000+ songs in an Android device. That is, Android's Audio.Media has 8,000+ rows.
That is, I have an array with 2,000 paths, and I need to query the database for other columns of corresponded files.
By now I use resolver.query(URI, "_data IN (?,?,?........,?)", selectionArgs, null);
But if the ? is too many like 600 or more, the query will fail since the WHERE clause becomes too long.
So I divide 2,000 paths into four parts, query each and save information needed in other place. With four times query, I can get the result. But this consumes lots of memory, and if I can do it by one query, it will be faster than 4.
Does anyone has better way to do this? Thank you!
Good evening ( for me ), I'm Emanuele a young android developer.
I want to share with you my problem because I'm not finding a good solution.
I've 3 tables (in reality 5)
posts
categories
posts_categories
to create the many-to-many relation between posts and categories
I've created a ListFragment to show all my posts with their categories.
So I've created this query ( I don't write all the code :) )
SELECT posts._id, posts.post_title, categories.category_name
FROM posts
LEFT OUTER JOIN posts_categories
ON posts_categories.post_id=posts.post_id
LEFT OUTER JOIN categories
ON posts_categories.category_id=categories.category_id;
So if I have 2 post with 2 category each that query will return 4 records. Am I wrong?
So in my Cursor I have 4 record but what I really need is to collect datas from the cursor and display only what I really need.
In this case I cannot use a CursorAdapter to display my data because it will insert 4 item in the ListFragment.
And I don't want to load only post and than for each post load categories because if I have 100 posts I will do 1 query to select all posts and 100 query to select categories.
What can I do? I need an expert advice! Which is the best way to handle this situation?
Have you ever faced this problem?
Thank you very much. Emanuele Ricci.
I suggest that you extend ArrayAdapter and provide your own ArrayList with your own Object for each result in your list. The ArrayAdapter provides a function called getView() which should be overridden and customized to inflate and display what you need. That should be all you need.
Did you tried CursorAdapter with Filterable. Here is [link] (http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/AutoComplete4.html). Hope this help.
The task is to combine data from 2 different tables with similar columns, sorted by one column .
Seems like MergeCursor might help, but have no idea how to sort items.
The only solutions I see now is converting manually to ArrayAdapter, or do sneaky JOINs (not sure yet its possible)
Thanks.
MergeCursor does not offer sorting.
The only solutions I see now is converting manually to ArrayAdapter, or do sneaky JOINs (not sure yet its possible)
I have no idea what the latter is. If you want to stick with the Cursor interface, you can build yourself a MatrixCursor. Or, you can try to create your own CursorWrapper that maintains the sort order and rewrites all position-related calls.