Reputation: 944
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-
Upvotes: 0
Views: 648
Reputation: 86948
The problem is with this:
Data.MIMETYPE + "='*'"
That is searching for MIMETYPE
s literally named *
. If you want to select all MIMETYPE
s, 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:
for(cursor.moveToFirst(); cursor.moveToNext(); cursor.isAfterLast()) {
Since cursor
has not been move from index -1. This will safely start at the beginning and iterate until the last row:
while(cursor.moveToNext()) {
If you have moved cursor
and want to start from the beginning again you can use:
for(cursor.moveToFirst(); cursor.moveToNext(); ) {
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 returns true
if the cursor is still in bounds. (In other words if cursor.isAfterLast()
is true
then cursor.moveToNext()
returns false
.)
Upvotes: 4