0

I'm not terribly familiar with the serialization of objects in java but I'm trying to insert an object that I serialized into a table that consists of 2 columns: sampleTimestamp INTEGER, data BLOB. Using the code:

// Binary Serialization of the object
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                ObjectOutputStream os = new ObjectOutputStream(bos);
                os.writeObject(result.getTopicInstance());
                byte[] byteArray = bos.toByteArray();
                os.close();

                String query = SQLiteInterface.getInstance().insertOI("data", "sampleTimestamp, data", "" + result.getTimestamp() + "," + byteArray);

public String insertOI(String tableName, String columnList, String values)
{
    return "INSERT OR IGNORE into " + tableName + "(" + columnList + ") values(" + values + ")";
}

The query returned from this is:

INSERT OR IGNORE into data(sampleTimestamp, data) values(1520448720940000000,[B@d32e4c)

The query indeed gets to SQLite and issued but returns the error:

[SQLITE_ERROR] SQL error or missing database (unrecognized token: "[B@d32e4c)")

This is likely a sqlite formatting error because it doesn't like the "[" being in the query. Edited based on comments below - This is the .toString representation of the byte array. How do I correctly get the byte array into an insert statement for SQLite?

12
  • 1
    B@ doesn't signify binary. [B signifies a one dimensional byte[] array. It's the default result of arrays' toString() and you don't want to save that in the database. Commented Mar 15, 2018 at 19:37
  • @Kayaman Added the code for the insertOI function. This is probably where it is getting casted.... Am I correct? Commented Mar 15, 2018 at 19:41
  • How would I insert it correctly? Commented Mar 15, 2018 at 19:44
  • Nothing's getting casted. You're converting it to a String at "" + result.getTimestamp() + "," + byteArray, but that won't work. Firstly because if you convert an array to a String like that, you just get something like [B@d32e4c, which has nothing to do with the contents of the array, and secondly because that's most certainly not the way to insert binary data. I recommend diving into the documentation to see how to insert binary data. Commented Mar 15, 2018 at 19:47
  • I see that it can be done this way with a PreparedStatement: stackoverflow.com/questions/29180820/… but can it be done with a normal Statement? Commented Mar 15, 2018 at 19:55

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.