comparing two byte arrays

129 views
Skip to first unread message

gmseed

unread,
May 1, 2013, 6:50:46 AM5/1/13
to xer...@googlegroups.com
Hi

When comparing two byte arrays [of type BLOB] is the correct method as follows:

"CREATE TABLE BytesTable(id INTEGER PRIMARY KEY NOT NULL, bytes BLOB)";

...
byte[] byteArray;
...
String query = "select * from BytesTable where bytes==byteArray";

Thanks

Graham

gmseed

unread,
May 1, 2013, 7:10:26 AM5/1/13
to xer...@googlegroups.com
Hi

Simply using the == operator I get the following exception:

java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (unrecognized token: "[B@e26fb32")


and if I use a prepared statement as follows:

            String selectSQL = "SELECT * FROM Bytes WHERE bytes=?";
            PreparedStatement pstmtSelectQuery = db_con.prepareStatement(selectSQL);
            pstmtSelectQuery.setBytes(1, bytes);
            rs = pstmtSelectQuery.executeQuery(selectSQL);
            while (rs.next())
            {   
                byte[] byteArray = rs.getBytes("bytes");
                System.out.println("byteArray read from db: " + ToStringBuffer.list(byteArray, "[", ", ","}"));          
            }

I get the exception:

java.sql.SQLException: not supported by PreparedStatment

Is there a solution to this query?

Graham

gmseed

unread,
May 1, 2013, 7:18:30 AM5/1/13
to xer...@googlegroups.com
Hi

Works fine if the no-argument version of executeQuery() is used with the prepared statement.

Graham

On Wednesday, May 1, 2013 11:50:46 AM UTC+1, gmseed wrote:

Grace B

unread,
May 2, 2013, 7:55:13 AM5/2/13
to xer...@googlegroups.com


On Wednesday, May 1, 2013 7:10:26 AM UTC-4, gmseed wrote:
Hi

Simply using the == operator I get the following exception:

java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (unrecognized token: "[B@e26fb32")


When writing SQL statements, objects need to be translated to SQL literals. 


and if I use a prepared statement as follows:

            String selectSQL = "SELECT * FROM Bytes WHERE bytes=?";
            PreparedStatement pstmtSelectQuery = db_con.prepareStatement(selectSQL);
            pstmtSelectQuery.setBytes(1, bytes);
            rs = pstmtSelectQuery.executeQuery(selectSQL);
            while (rs.next())
            {   
                byte[] byteArray = rs.getBytes("bytes");
                System.out.println("byteArray read from db: " + ToStringBuffer.list(byteArray, "[", ", ","}"));          
            }

I get the exception:

java.sql.SQLException: not supported by PreparedStatment 

Is there a solution to this query?


This is a bug in driver.  
Reply all
Reply to author
Forward
0 new messages