0

I need to query a ContentProvider based on the value of a binary blob column:

mContext.getContentResolver().query(
               contentUri,                          // Uri
               null,                                // Projection
               TableColumns.blobColumn + " = ?",    // Selection
               new String[] { blob_query },         // Selection Arguments
               null);                               // Order

Based on my reading of SQLite literal values it seems a blob with value [0x01] may be queried with the string literal X'01'.

However I'm unable to query a record where blobColumn has value new byte[] { (byte) 0x01 }

Test code

 // Insert record
 byte[] blobData = new byte[] { (byte) 0x01 };

 ContentValues entry = new ContentValues();
 entry.put(TableColumns.blobColumn, blobData);
 Uri entryUri = getContext().getContentResolver().insert(contentUri, entry);

 // Query record by blob (fails)
 result = getContext().getContentResolver().query(contentUri,
            null,
            TableColumns.blobColumn + " = ?",
            new String[] {
              "x'01'"
            // None of the above or below work
            // "0x01"
            //"X'01'"
            //"01"
            },
            null);
 result.getCount() // 0

 // Query record by id (succeeds)
 result = getContext().getContentResolver().query(contentUri,
            null,
            TableColumns.idColumn + " = ?",
            new String[] {
              entryUri.getLastPathSegment()
            },
            null);
 result.getCount() // 1
1
  • 1
    Note that blob columns are not indexed so doing this kind of query requires iterating over every row in the database...not something you normally want to do. Oct 19, 2014 at 22:28

1 Answer 1

0

The following works:

    Cursor result = getContext().getContentResolver().query(contentUri,
            null,
            "quote(" + TableColumns.blobColumn + ") = ?",
            new String[] { "X'01'" },
            null);

I'll happily accept a better answer if it exists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.