I have an issue where when I execute my query with a WHERE clause it returns no results but if I leave the WHERE as null it will execute through all of my records and find the match. Can anyone tell me what is wrong with my query?
in my code lets say that contactURI is equal to “content://com.android.contacts/contacts/lookup/953i7d73a639091bc5b6/164” and contactUriId is equal to “164”
// Put data in the contactURI
contactURI = data.getData();
// get the contact id from the URI
contactUriId = contactURI.getLastPathSegment();
//TEST TODO Fix URI Issue
String [] PROJECTION = new String [] { Data.CONTACT_ID, Data.LOOKUP_KEY };
Cursor cursor = this.managedQuery(Data.CONTENT_URI, PROJECTION, Data.CONTACT_ID + "=?" + " AND "
+ Data.MIMETYPE + "='*'",
new String[] { String.valueOf(contactUriId) }, null);
Log.e(DEBUG_TAG, contactUriId+"-164-");
for(cursor.moveToFirst(); cursor.moveToNext(); cursor.isAfterLast()) {
Log.v(DEBUG_TAG, "lookupKey for contact: " + cursor.getString(1) + " is: -" + cursor.getString(0) + "-");
if(cursor.getString(0).equals("164")){
Log.e(DEBUG_TAG, "WE HAVE A MATCH");
}
}
This is my log…
05-14 19:08:40.764: D/OpenGLRenderer(21559): Flushing caches (mode 0)
05-14 19:08:46.944: E/MyDebug(22269): 164-164-
The problem is with this:
That is searching for
MIMETYPEs literally named*. If you want to select allMIMETYPEs, don’t do anything. A query returns all of these rows by default.Also, this loop seems to execute, but it is not quite right:
Since
cursorhas not been move from index -1. This will safely start at the beginning and iterate until the last row:If you have moved
cursorand want to start from the beginning again you can use:Detailed Explanation Why:
cursor.isAfterLast()does not move the cursor, it simply checks if the cursor is still referring to valid data.cursor.moveToNext()moves the cursor to the next row and returnstrueif the cursor is still in bounds. (In other words ifcursor.isAfterLast()istruethencursor.moveToNext()returnsfalse.)