1
votes

I have looked at this for a couple of days now and I completely can't work out why my content provider return 0 using the arguments I am passing it.

Here's my contentResolver code:

String[] expenditureProjection = {
            BusinessOpsDatabase.COL_EXPEND_CAT_ID,
            BusinessOpsDatabase.COL_EXPEND_DATE,
            BusinessOpsDatabase.COL_EXPEND_AMOUNT,
            BusinessOpsDatabase.COL_EXPEND_DESC,
            BusinessOpsDatabase.COL_STERLING_EXCHANGE,
            BusinessOpsDatabase.COL_COMPANY_ID,
            BusinessOpsDatabase.CURRENCY_ID,
            BusinessOpsDatabase.COL_MOD_DATE
    };
    // Defines a string to contain the selection clause
    String selectionClause = null;

    // An array to contain selection arguments
    String[] selectionArgs = {expend_id.trim()};

    selectionClause = BusinessOpsExpenditureProvider.EXPENDITURE_ID + "=?";

    Log.d(TAG, expend_id+" Selected from list.");

    Cursor expendCursor = getContentResolver().query(
            BusinessOpsExpenditureProvider.CONTENT_URI, expenditureProjection, selectionClause, selectionArgs, null);
    if (null == expendCursor) {
        Log.d(TAG, "Expenditure cursor: Is null");
    } else if (expendCursor.getCount() < 1) {
        Log.d(TAG,"Expenditure cursor: Search was unsuccessful: "+expendCursor.getCount());
    } else {
        Log.d(TAG,"Expenditure cursor: Contains results");

        int i=0;
        expendCursor.moveToFirst();
        // loop through cursor and populate country array
        while (expendCursor.isAfterLast() == false)
        {
            expend_date_edit.setText(expendCursor.getString(1));
            expend_amount_edit.setText(expendCursor.getString(3));
            expend_desc_edit.setText(expendCursor.getString(4));

            i++;
            expendCursor.moveToNext();
        }
    }

Here's my content provider query method:

@Override
public Cursor query(Uri uri, String[] projection, String selection,
                    String[] selectionArgs, String sortOrder) {
    SQLiteDatabase db = mDB.getWritableDatabase();

    // A convenience class to help build the query
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

    qb.setTables(BusinessOpsDatabase.TABLE_EXPENDITURE);

    switch (sURIMatcher.match(uri)) {
        case EXPENDITURE:
            if(selection != null && selectionArgs != null){
                //values.get("company_contact");
            String segment = uri.getLastPathSegment();
            Log.d(TAG, "Last path segment: "+ segment);
            String whereClause = BusinessOpsDatabase.EXPENDITURE_ID + "="+ selectionArgs[0];
            Log.d(TAG, "Where clause: "+whereClause);
            }
            break;
        case EXPENDITURE_ID:
            // If this is a request for an individual status, limit the result set to that ID
            qb.appendWhere(BusinessOpsDatabase.EXPENDITURE_ID + "=" + uri.getLastPathSegment());
            break;
        default:
            throw new IllegalArgumentException("Unsupported URI: " + uri);
    }


    // Query the underlying database
    Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, null);

    // Notify the context's ContentResolver if the cursor result set changes
    c.setNotificationUri(getContext().getContentResolver(), uri);

    // Return the cursor to the result set
    return c;
}

I'm printing the whereclause to the log and I see '_id=3' which should be fine because I have pulled off a copy of my SQLite database and I can see that the expenditure table has an _id 3 row in it. Any Ideas?

1
What is the type of the _id? if it is of type String, the where clause should look like _id = '3' - Saeid Farivar
Thanks for the reply. The _id is of type integer. I have tried what you suggest and I get the same result. - user2670815
Is there an easy way to see the whole query string being executed on the SQLite db? - user2670815
you would want to look here. I recommend using compile statement. then if the statement is compiled successfully you can execute it. besides you get to know what is the query. it might not be that easy thu. - Saeid Farivar
Given how many columns I want to select, it looks like the SQLiteStatement Class which is mentioned in the linked article isn't suitable. "Represents a statement that can be executed against a database. The statement cannot return multiple rows or columns, but single value (1 x 1) result sets are supported." - user2670815

1 Answers

0
votes

What an epic problem this has been. I found the error in my ContentResolver code.

selectionClause = BusinessOpsExpenditureProvider.EXPENDITURE_ID + "=?";

I was using the EXPENDITURE_ID variable from the provider rather than the database class. The line now reads.

selectionClause = BusinessOpsDatabase.EXPENDITURE_ID + "=?";

And works!