Select first n rows in contentProvider - android

I've this select:
final Cursor cursorConversations = getContentResolver().query(
Uri.parse("content://gmail-ls/conversations/" + Uri.encode(mailAddress)),
null, null, null, BaseColumns._ID + " DESC");
ContentQueryMap mQueryMap
= new ContentQueryMap(cursorConversations, BaseColumns._ID, true, null);
With ContentQueyMap I can cache Cursor data and iterate in it also with the Cursor closed (i need it to boost performance).
Now, I want that the select of the Corsor only retrieve the first fifty rows. The solution of looping for 50 times in mQueryMap.getRows().entrySet() is not right: I don't want that mQueryMap gets all the rows of the Cursor, but only the first fifty.
Any idea? Does exist a where clause to get only first n rows?

You could do
final Cursor cursorConversations = getContentResolver().query(
Uri.parse("content://gmail-ls/conversations/" + Uri.encode(mailAddress)),
null, null, null, BaseColumns._ID + " DESC " + " LIMIT 5");
"LIMIT x" after your SORT.
Cheers

SELECT * FROM Table_Name LIMIT 5;
That's valid for sqlite. Try adding "LIMIT 5" to your where clause. (I have not tried)

Related

Get the Max _id from ContentResolver from Contacts.CONTENT_URI

I want to get the last added contact in the contacts and for getting that i want to pull the max _id of the contact. So here is my query that i want to realise:
AppMain.applicationContext.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
new String[]{
"MAX(" + ContactsContract.Contacts._ID + ") as max_id",
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER
},
null, null, null);
But unfortunately i am getting this error:
Invalid column MAX(contact_id) as max_id
I tried removing the 'as max_id' but no luck.
Does any one know how to get the last added contact or get the max _id of the contact.
This is not officially supported by Android's ContentProvider framework, especially if the Provider set the strictProjectionMap flag.
But this should work instead, it asks for all contacts sorted by contact-id, and limits the results to 1:
Cursor c = cr.query(Contacts.CONTENT_URI, new String[] { Contacts._ID }, null, null, Contacts._ID + "DESC LIMIT 1");
if (c != null && c.moveToNext()) {
Log.d(TAG, "id is: " + c.getLong(0));
}

how to query sqlite when there are more than one condition that needs to be met

I am using a content provider and need to query a table where one column of the row = thing1 and another column of that same row = thing2.
i know you can query a table for just one condition like:
Cursor c = contentResolver.query(content_uri, projection, variable + "='" + thing1 + "'", null, null);
now how can i query for two conditions? any help would be appreciated.
This should work:
Cursor c = contentResolver.query(content_uri, projection, "col1=? AND col2=?", args, null);
where args is a String[] of values to substitute in for the ? in the query.
Try this
Cursor c = contentResolver.query(content_uri, projection, variable + "='" + thing1 + "' AND "+variable2 + "='"+thing2+"'" , null, null);

android sql - how do you order your sql query by multiple columns

I am currently working on an Android project in Eclipse and i am having problems with my SQL query.
I am trying to order the query by more than two columns, currently i am doing it by KEY_DAY_ID but i want to also do it by KEY_START_TIME, but i can't get it to work
my query currently looks like this:
Cursor cursor = db.query(TABLE_SESSION, new String[] {KEY_ID, KEY_MODULE_CODE,
KEY_DAY_OF_WEEK, KEY_START_TIME, KEY_DURATION, KEY_SESSION_TYPE, KEY_ROOM},
null, null, null, null, KEY_DAY_ID + " ASC");
Please let me know your thoughts. Thank you in advance!
The last parameter in db.query() method is the order by clause (without the "order by"). All you need to do is separate both columns by a ",". So it would look like:
Cursor cursor = db.query(TABLE_SESSION, new String[] {KEY_ID, KEY_MODULE_CODE,
KEY_DAY_OF_WEEK, KEY_START_TIME, KEY_DURATION, KEY_SESSION_TYPE, KEY_ROOM},
null, null, null, null, KEY_DAY_ID + " ASC, " + KEY_START_TIME + " ASC");
This works for me
SQLiteCursor cursor = (SQLiteCursor) db.query(DbHelper.TIMES, colmn, null, null, null, null, DbHelper.TABLE_DAY + " ASC, " + DbHelper.TABLE_LECTURE_NO + " ASC",null);
Also you can do it in select line like this:
Cursor data = ddbb.rawQuery("SELECT * FROM vacations ORDER BY NAME ,MONTH , date ",null);
in previous code the first probability for the first column "NAME" then will start arrange by the Second probability "MONTH" then the third "date".....
which mean working in series
Or:
Cursor data = ddbb.rawQuery("select * from vacations where NAME = ? ORDER BY MONTH AND date ",new String[]{ns});
in previous code by using "AND" the two conditions are working together in parallel

usage of select statement in sqlite

Cursor mCursor = mDb.query(true, CUSTOMER_TABLE_NAME,
new String[] {GENERIC_ID_KEY, ADDRESS_KEY, PHONE_KEY, EMAIL_KEY,CUSTOMER_NAME_KEY},
GENERIC_ID_KEY + "=" + customerDbId, null,
null, null, null, null);
How should I modify this command if GENERIC_ID_KEY and customerDbId is string.
I tried using like in b/w but still negative results, n error is thrown.
if customerDbId is a string type then place it in between quote
as
GENERIC_ID_KEY + "= '" + customerDbId + "'"
It's a cursor problem. Be sure curser is the first state before selection.

Loading phone contacts in ascending order

I am trying to load phone contacts and tried to show the contact names in ascending order. My code is given below:
Cursor cursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null,
ContactsContract.Contacts.HAS_PHONE_NUMBER + " = 1", null,
ContactsContract.Contacts.DISPLAY_NAME + " ASC");
I got the required output. But a problem is there, names staring with small letter is shown as last one. First the capital letters are sorted, only after that contact names staring with small letters is shown. PLS HELP
OUTPUT IS:
Alfin A
Bipin B
Calvin C
Jobin
Shine
anurag U
shine H
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null,
ContactsContract.Contacts.HAS_PHONE_NUMBER + " = 1",
null,
"UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");

Categories

Resources