Android column '_id' does not exists - android

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..

Related

CalendarContract.Events._ID is missing in Android 5.0

My app queries a particular event in CalendarContract.Events using Events._ID. This worked well until attempting to run it on a 5.0 device, now I get an exception
01-12 17:28:50.525: E/Unknown Source(18499): android.database.sqlite.SQLiteException:
no such column: CalendarContract.Events._ID (code 1): ,
while compiling:
SELECT _id, account_type, title, organizer, description, eventLocation,
hasAlarm, calendar_id
FROM view_events
WHERE (lastSynced = 0 AND (CalendarContract.Events._ID=1))
Querying all columns in Events indeed does not return _ID. Any idea why this has been removed or if it's a bug? I can't seem to find away to uniquely identify events any more.
Here is my query:
String[] projection = new String[]{Events._ID, Events.ACCOUNT_TYPE, Events.TITLE,
Events.ORGANIZER, Events.DESCRIPTION, Events.EVENT_LOCATION, Events.HAS_ALARM,
Events.CALENDAR_ID};
Cursor cursor = context.getContentResolver().query(EVENTS_CONTENT_URI, projection,
"CalendarContract.Events._ID=" + eventId, null, null);
Thanks for any information!
The answer of #Andrew isn't right.
You are wrongly using the selectionClause and the selectionArgs parameters.
Here is what you are doing:
Cursor cursor = context.getContentResolver().query(EVENTS_CONTENT_URI, projection,
CalendarContract.Events._ID + "=" + eventId, null, null);
And here is what you should do:
Cursor cursor = context.getContentResolver().query(EVENTS_CONTENT_URI, projection,
CalendarContract.Events._ID + " = ?", new String[]{eventId}, null);
The eventId needs to be passed within the selectionArgs so that the contentResolver can build the query statement for you, chaining the selectionArgs and substituting them to the ? char in the selectionClause parameter.
The question is three years old, but I hope my answer can help someone else.

Querying DISTINCT records from existing content providers in android

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");

The infamous 'no such column _id' for SimpleCursorAdapter

So yeah, I've seen questions about this all over the place, and so have accordingly added an _id alias to my query as below:
SimpleCursorAdapter sca = new SimpleCursorAdapter(getActivity(),
R.layout.activity_contact_list, cursor, new String[] {
"rowid _id", DBOps.COL_CATNAME },
new int[] { R.id.contact_list }, CursorAdapter.NO_SELECTION);
I'm creating my cursor like so:
public Cursor getAllCategories() {
return mDB.query(TABLE_NAME, new String[] { "rowid _id", COL_ID,
COL_CATNAME,
COL_ICONPATH }, null, null, null, null, null);
}
mDB in the above is a SQLite database.
I've tried changing the string to rowid as _id, which also doesn't work. Also apparently there's no need to change my table structure by adding another _id column as a few others have noted, so where am I going wrong here?
Update - here's the stack trace -
Caused by: java.lang.IllegalArgumentException: column 'rowid _id' does not exist
at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
at android.widget.SimpleCursorAdapter.findColumns(SimpleCursorAdapter.java:333)
at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:107)
at com.rex.bizcontacts.ContactListFragment.onCreate(ContactListFragment.java:77)
No, you're not getting the problem.
The comment you are complaining about has the right idea. rowid _id is not a valid name. You can tell that by looking at the exception.
You are welcome to try "rowid AS _id" instead of "rowid _id". I would recommend rawQuery() rather than query(), so this can be written more naturally ("SELECT rowid AS _id, ...").

Android application contact groups

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

ContactsContract.Data implicit-join columns not working?

Sorry if this might be a duplicate question, I've spent the evening trying to wrap my head around this, and I can't seem to find other posts that might cast some light on this as well, so I am hoping that a few more pair of eyes might spot something.
I am having this impression from the API docs for ContactsContract.Data that when you specify certain fields, the library does some magic and performs an implicit join for you in the background.
Doesn't seem to be working for me.
import android.provider.ContactsContract.CommonDataKinds.Phone;
private Cursor getContacts()
{
// Run query
Uri uri = Phone.CONTENT_URI;
String[] projection = new String[] {
Phone.DISPLAY_NAME,
Phone.NUMBER,
Phone.CONTENT_ITEM_TYPE,
Phone.HAS_PHONE_NUMBER,
Phone.IN_VISIBLE_GROUP
};
String selection = Phone.HAS_PHONE_NUMBER + " = '1' AND " + Phone.IN_VISIBLE_GROUP + " = '1'";
String[] selectionArgs = null;
String sortOrder = Phone.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
return getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
}
When this is run, it dies with a:
java.lang.IllegalArgumentException: Invalid column vnd.android.cursor.item/phone_v2
From the docs for ContactsContract.CommonDataKinds.Phone it clearly states that:
You can use all columns defined for ContactsContract.Data as well as the following aliases.
What am I missing?
Phone.CONTENT_ITEM_TYPE is your problem. That's not a column name, that's a constant that Data.MIME_TYPE is set to. Remove it from your projection and it should be fine.

Categories

Resources