I want Distinct records from content provider.
My current code:
String[] projection= {"_id","address"};
Cursor address = getContentResolver().query(android.provider.Telephony.Sms.Inbox.CONTENT_URI,projection, null, null,"address ASC");
You can add DISTINCT keyword in columns names in projection. e.g.
String[] projection= {"DISTINCT _id"};
Hope this will help you.
//use the DISTINCT before the name of the column
String[] projection= { "DISTINCT _id", "DISTINCT address"};
Cursor address = getContentResolver().query(android.provider.Telephony.Sms.Inbox.CONTENT_URI,projection, null, null,"address ASC");
Related
i want to filter multiple data such as
id = "1,3,5" from columnid which is having 1 to 10 id
and another column such as name
name = "a,e,d" from name column of 10 records
and another criteria such as age
age = "21,23,20" from age column of 10 records from same table,
one example i got is
Cursor cursor = db.query("TABLE_NAME",new String[]{"ColumnName"}, "ColumnName=?",new String[]{"value"}, null, null, null);
which is just for one column but i want to get data from multiple column, can anyone help me?
try this working example,
Cursor cursor =
db.query(TABLE_DIARYENTRIES,
new String[] {},
STUDENT_ID + " IN ("+resultStudent+")"+ " AND " +CLASS_NAME + " IN ("+resultClass+")"
+ " AND " +SUBJECT_NAME + " IN ("+resultSubject+")"
null, null, null, null);
and your result string should be 'a','b','c'
I really like the way Google's example is structured. Because for noobies such as myself it makes it really clear what I am doing. And it is also more robust to SQL injections. Here is my modified version of the Google example:
//Column(s) I want returned
String[] projection = {"ColumnIWantReturned"};
//Column(s) I want to filer on
String selection = "FilterColumn1 IN (?) and FilterColumn2 IN (?, ?)";
String[] selectionArgs = {"ArgumentForFilterColumn1", "FirstArgumentForFilterColumn2", "SecondArgumentForFilterColumn2"};
Cursor cursor = db.query(
"MyTable", // The table to query
projection, // The array of columns to return (pass null to get all)
selection, // The columns for the WHERE clause
selectionArgs, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
null // The sort order
);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Log.d("this-is-a-test", cursor.getString(0));
cursor.moveToNext();
}
cursor.close();
I'm making an Android App with a sqlite db, which has a query method like this:
String[] projection = {COLUMN_NAME_ID};
String selection = COLUMN_NAME_TEAM + " = ?";
String[] selectionArgs = {teamId.toString()};
String sortOrder = COLUMN_NAME_NAME + " ASC";
Cursor c = db.query(TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
if (c.moveToFirst()){
//...
}
as suggested here. The point is to implement this query:
SELECT PLAYER_ID FROM PLAYERS WHERE TEAM_ID = ? ORDER BY NAME ASC;
According to the SQLiteDatabase.query documentation, this is a valid call to query(), and so far I've been doing it successfully with all my other queries in this App.
For some reason, in this query (and only in this) the App freezes in c.moveToFirst().
Some answers to similar questions suggest that it might be a performance issue. I don't think this is the case, as my table has only 6 rows and the query is quite simple.
Any ideas?
Thanks
I need phone contacts to be divided into groups in my application, but only in my application so i don't want to add any groups to original phone contact database. I have made my own database to have my groups table:
groups(group_id int, group_name text, message text)
and contacts_group table.
contacts_group(contact_id, group_id)
Now the problem i am facing: How should i create the query to get all contacts from my group (from phone contact databse).
I need something like this: Select ...DISPLAY_NAME, ...NUMBER where ..._ID in (String[] ids) while String[] ids is an array of contact_ids from contacts_group table. Is it possible to put my string array in '?' in raw query? As.. Select... where .._ID in ?, string[] ?
Thanks for help in advance
Regards
Probelm solved. Solution:
public static Cursor getContactsFromGroup(String[] ids, SQLiteDatabase db) {
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = { ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER };
Cursor c = context.getContentResolver().query(uri, projection,
"_ID IN (" + makePlaceHolders(ids.length) + ")", ids, null);
}
where makePlacHoldes(int) puts "?" chars, as many as long is param array.
Regards
Previously I selected everything by putting a null, but i get an error complaining that it has too many rows, so I'm trying to get a projection of the display name, contact id and phone number but getting the following error:
"column '_id' does not exist"
Here's the code:
final String[] projection = { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.CONTACT_ID };
Cursor cur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection,
"LENGTH(" + ContactsContract.CommonDataKinds.Phone.NUMBER
+ ") >= 8 ) GROUP BY ("
+ ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, null,
sortOrder);
But the error points to this line in my customadapter
super(context, layout, c, from, to);
Need help! Thanks!
Add BaseColumns._ID to your projection array.
android needs the _id column, so include your column with private key in your query.
you can rename the result column with something like this:
"select PRIMARYKEYCOLUMN as _id,BLA,BLUBB from TABLE"
See your query. You may write column_id in place of contact_id by mistake else it will not show this type error..
I know how to retrieve contact data for specific contacts. However, i can't find a way to get all contacts plus some of their details in a single query. The following code gets all contacts having a postal address:
Uri uri = ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI;
String[] projection = new String[] {
StructuredPostal._ID,
StructuredPostal.LOOKUP_KEY,
StructuredPostal.DISPLAY_NAME,
StructuredPostal.STREET,
StructuredPostal.CITY
};
String sortOrder = StructuredPostal.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor c = getContentResolver().query(uri, projection, null, null, sortOrder);
But what i need are all contacts, whether they have postal address or not. Is this doable using the ContatsContract API, or do i need to create custom outer join query? Any hints on how?
if You have a contact ID and you want to fetch the Postal Address then use this :
Uri postal_uri = ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI;
Cursor postal_cursor = getContentResolver().query(postal_uri,null, ContactsContract.Data.CONTACT_ID + "="+contactId.toString(), null,null);
while(postal_cursor.moveToNext())
{
String Strt = postal_cursor.getString(postal_cursor.getColumnIndex(StructuredPostal.STREET));
String Cty = postal_cursor.getString(postal_cursor.getColumnIndex(StructuredPostal.CITY));
String cntry = postal_cursor.getString(postal_cursor.getColumnIndex(StructuredPostal.COUNTRY));
}
postal_cursor.close();
All contact information in Android 2.0 is stored in a single database table. So you can get all the information you need in a single query:
Cursor c = getContentResolver().query(ContactsContract.Data.CONTENT_URI,
null, null, null, sortOrder);
The just iterate through the data and check Data.MIMETYPE column. For example, if this column has StructuredPostal.CONTENT_ITEM_TYPE value, then you can get StructuredPostal fields from this column.