Distinct and substri in android with sqlite - android

I want get the first letter for all contacts without repetition i can't use something like this:
Cursor flc = this.cr.query(ContactsContract.Contacts.CONTENT_URI,
Proyection2, null, null, "UPPER("+ContactsContract.Contacts.DISPLAY_NAME+")");
Because Content providers don't allow functions in proyection with code this is explanation that i want:
"Select distinct substr(" + ContactsContract.Contacts.DISPLAY_NAME+",
1, 1) from "+ContactsContract.Contacts.CONTENT_URI "
I search a method for do this with content providers or accesing directly to bd but i don't know how.

Content providers are not a generic SQL interface; they support only those accesses that they have implemented, and they might not be based on an SQL database in the first place.
If you want to do any filtering or grouping not directly supported by a content provider, you have to do it yourself.

Related

Android Content Provider - Using Query Method To Insert, Delete, and Update

I have been studying Content Providers by reading Android Developer Fundamentals (Version 1).
Under section "11.1 Share Data Through Content Providers" / "The query() method" there is a note that states
Note: The insert, delete, and update methods are provided for
convenience and clarity. Technically, the query method could handle
all requests, including those to insert, delete, and update data.
How can query method be used to insert / delete / and update data? The query method has the following signature and does not take in custom SQL strings.
public Cursor query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder){}
I have not been able to find any resources that shows how this can be done. All seems to utilize the provided methods.
Frankly, that is a really poor job in that training guide. All it will do is confuse people.
I presume what they mean is that there is nothing magic about query() that forces it to only perform SQL SELECT statements. A ContentProvider is a facade — you can use it to store and retrieve from pretty much whatever you want, not just SQLite.
So, you could have your query() method:
Examine the Uri
Look for it to be of the form content://your.authority/something/insert/put/data/here
Parse the Uri to get the put, data, and here values
Insert those in a table under some pre-determined columns
Return an empty MatrixCursor
Or, it could:
Examine the Uri
Look for it to be of the form content://your.authority/something/insert
Insert a row using the projection for the columns and the selectionArgs as the values to put in those columns
Return an empty MatrixCursor
I do not know why anyone would do that, but it is certainly possible.

Android Cursor retrieve belongsTo relationshoips

I have two data models Item and Category with the relationship "Item belongs to Category"
Naturally my database has an Item.category_id column.
When I retrieve data using SQLiteDatabase#query() it returns a cursor with the data for the passed table name. How can it also retrieve related data (in this case, the category)?
PS: I'm trying to avoid rawQuery() but if that's the only way then...
This can be achieved with a regular Query. I'm making the assumption you have the id of the item and want to fetch the Category.
I'm also making the assumption you know what I mean by/how to create your own Contract & Provider, if you don't, look up database providers on the official docs or let me know
String[] args = { String.valueOf(myItemId) };
getContentResolver().query(MyCategoryContact.CONTENT_URI, null, MyCategoryContact.ITEM_ID + " = ?", args, null);

Content Provider filtering query, filtering Cursor

I got following problem, I need to use a Content Provider to read a
Database of an other App.
first I want all rows, and after analyzing the data only e.g. the rows
from _id = 1, 3 and 5.
how can I call a Content provider and select only these rows?
or is it possible to create a subset Cursor form an given Cursor?
Thanks in advance.
If you're talking to another app, I assume you're querying the other app's ContentProvider to get the data from them in the first place.
In this situation, the cleanest answer seems not to build your own ContentProvider that filters/wraps theirs. Instead query their ContentProvider from your application directly, and use the select clause in your query() to specify the conditions that define the subset of data you want to be given.

How to join a ContentResolver Cursor with a Database Cursor?

I get records from the system by quering a ContentResolver. I maintain the order of the items in the database. So I want to display the items in the order taken from my database.
How do I merge these two informations?
EDIT 1
I am looking after an alternative way now. As what I ideally want is:
get order of contacts by a custom order held in my database (this involves joining CR with my DB cursor, and doing an order by, later seams it's not possible with CursorJoiner)
but there is more, if the join is not unique I want to sort by contact's name as last measure
which is impossible using Cursor and Joiners, because of the missing feature of order bys, also I need to return a Cursor, as I will be using the in an ExpandableList
Also this translated to TSQL it would look like
select * from contactsdata
left join category on contactsdata.catid=category.id
order by category.pos asc, contact.display_name asc
So I am looking now after an alternative.
I have in mind to load in a temporary DB table all data from CR, then do the query on the temporary table where I can join tables and do order bys? How does this sound to you?
Take a look at CursorJoiner.
If that doesn't work, you can roll your own equivalent with a fair amount of pain, whiskey, or both.

how add limit clause to manageQuery on android

Android's API provides a clean mechanism via SQLite to make queries into the contact list. However, I am not sure how to limit the results:
Cursor cur = ((Activity)mCtx).managedQuery(
People.CONTENT_URI,
columns,
"LIMIT ? OFFSET ?",
new String[] { Integer.toString(limit), Integer.toString(offset) },
null
);
Doesn't work.
Actually, depending on the provider you can append a limit to the URI as follows:
uri.buildUpon().appendQueryParameter("limit", "40").build()
I know the MediaProvider handles this and from looking at recent code it seems you can do it with contacts too.
You are accessing a ContentProvider, not SQLite, when you query the Contacts ContentProvider. The ContentProvider interface does not support a LIMIT clause directly.
If you are directly accessing a SQLite database of your own, use the rawQuery() method on SQLiteDatabase and add a LIMIT clause.
I found out from this bug that Android uses the following regex to parse the LIMIT clause of a query:
From <framework/base/core/java/android/database/sqlite/SQLiteQueryBuilder.java>
LIMIT clause is checked with following sLimitPattern.
private static final Pattern sLimitPattern = Pattern.compile("\\s*\\d+\\s*(,\\s*\\d+\\s*)?");
Note that the regex does accept the format offsetNumber,limitNumber even though it doesn't accept the OFFSET statement directly.
I think you have to do this sort of manually. The Cursor object that is returned from the managedQuery call doesn't execute a full query right off. You have to use the Cursor.move*() methods to jump around the result set.
If you want to limit it, then create your own limit while looping through the results. If you need paging, then you can use the Cursor.moveToPosition(startIndex) and start reading from there.
You can specify the "limit" parameter in the "order" parameter, maybe even inside other parameters if you don't want to sort, because you'll have to specify a column to sort by then:
mContentResolver.query(uri, columnNames, null, null, "id LIMIT 1");

Categories

Resources