Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find byte[] in mysql query?

I have a nice MySQL table like this:

CREATE TABLE IF NOT EXISTS BLOBTest(Id INT PRIMARY KEY AUTO_INCREMENT,

    Data BLOB);

And inside it I have the following:

SELECT HEX(Data) from BLOBTest;

+----------------------------------+
| HEX(Data)                        |
+----------------------------------+
| E764DF04463B55E9E2305934266227A1 |
+----------------------------------+

Translated, I have a table with 1 row and two columns (Id and Data). This row holds byte[] data, the byte[] array stored is the following: [-25, 100, -33, 4, 70, 59, 85, -23, -30, 48, 89, 52, 38, 98, 39, -95]

How do I make a query to get the entire how? At first I tried:

SELECT * FROM BLOBTest WHERE Data='[-25, 100, -33, 4, 70, 59, 85, -23, -30, 48, 89, 52, 38, 98, 39, -95]';

However this approach does not work at all, it always returns an empty set. I need to get all the information of that row and all I have is the byte[] value. How do I do it?

Thanks for any help if possible plz.

EDIT:

Here is the code of the java app and what it does.

public Map<Integer, String> queryAuthor(String author) throws Exception{

    //encrypts the name of the author
    byte[] cipheredName = cryptManager.encrypt(keyBytes, 
                author.getBytes());

    //queries db for encrypted name
    pst = con.prepareStatement("SELECT * FROM BLOBTest WHERE DATA='" + cipheredName+"'");

    //builds a map for clients to use with the information from the query. 
    ResultSet rs =  pst.executeQuery();
    Map<Integer, String> result = new HashMap<Integer, String>();
    while (rs.next()) {

        byte[] recoveredBytes = rs.getBytes(2);

        byte[] recoveredName = cryptManager.decrypt(keyBytes, 
                    recoveredBytes);

        result.put(rs.getInt(1), new String(recoveredName));

    }

    return result;
}

The problem is that making:

SELECT * FROM BLOBTest WHERE Data='[-25, 100, -33, 4, 70, 59, 85, -23, -30, 48, 89, 52, 38, 98, 39, -95]';

Returns an empty set, thus making the variable "rs" empty and therefore the returned map is empty. Everyone is sad because I'm a noob and can't make a simple query like this =(

like image 312
Flame_Phoenix Avatar asked Dec 11 '25 14:12

Flame_Phoenix


1 Answers

You can send the raw bytes to MySQL as a string literal:

SELECT * FROM BLOBTest WHERE Data = '????????????????'

(Since not all of your bytes can be represented by printable characters, I haven't even tried).

This is particularly easy to do if you pass your data by way of a parameter to a prepared statement (which you really should be doing in any event):

pst = con.prepareStatement("SELECT * FROM BLOBTest WHERE DATA = ?");
pst.setBytes(1, cipheredName);

Otherwise you must be careful to ensure that you escape any bytes that MySQL would parse as string termination quotes, as they will otherwise corrupt your query.


(The following predates the additional information provided in the OP's edit)

If that had not been possible, it'd probably have indicated that you shouldn't be storing your data in a BLOB-type column; for example, 16 TINYINT SIGNED columns might have been more appropriate.

Even so, there are still possibilities:

  • If you can use Hexadecimal Literal in MariaDB or MySQL , which interprets the hex string to its binary form :

      SELECT * FROM BLOBTest WHERE Data = x'E764DF04463B55E9E2305934266227A1'
    

See it on sqlfiddle.

  • UNHEX() also does the same thing as described in Hexadecimal Literal above :

    SELECT * FROM BLOBTest WHERE Data = UNHEX('E764DF04463B55E9E2305934266227A1')
    
  • If you can convert each byte to its unsigned value:

      SELECT * FROM BLOBTest WHERE Data = CHAR(
        231,100,223,4,70,59,85,233,226,48,89,52,38,98,39,161
      )
    

See it on sqlfiddle.

  • Otherwise, you will need to strip the highest order bits as MySQL treats each input to CHAR() as a 4-byte integer:

      SELECT * FROM BLOBTest WHERE Data = CHAR(
        -25 & 0xff,
        100 & 0xff,
        -33 & 0xff,
          4 & 0xff,
         70 & 0xff,
         59 & 0xff,
         85 & 0xff,
        -23 & 0xff,
        -30 & 0xff,
         48 & 0xff,
         89 & 0xff,
         52 & 0xff,
         38 & 0xff,
         98 & 0xff,
         39 & 0xff,
        -95 & 0xff
      )
    

See it on sqlfiddle.

like image 146
eggyal Avatar answered Dec 14 '25 04:12

eggyal