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?
B@doesn't signify binary.[Bsignifies a one dimensionalbyte[]array. It's the default result of arrays'toString()and you don't want to save that in the database.Stringat"" + 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.