android – Storing Serialized Objects in SQLite


Faced an interesting glitch while storing a serialized object in SQLite

There is a class that can serialize and deserialize objects. If we take an array of strings, for example, and perform the serialization operation and vice versa, we get, as expected, the initial array of strings. But if you serialize the array, save it in the database, get it from there, then we already get an excellent sequence of bytes, respectively, the deserialization operation is performed with an error.

String [] anyarraystrings = {"26", "25", "21"}; byte [] bytec = Serilize.serializeObject (anyarraystrings); cv.put ("columnn", bytec);

    rowID = db.insert("pro", null, cv);
    Cursor c = db.query("pro", null, "_id = ?", new String[] {String.valueOf(rowID)}, null, null, "last_d DESC", "1");
    if (c.moveToFirst()){
        byte[] bytec2 = c.getBlob(c.getColumnIndex("columnn"));
        Log.d(LOG_TAG, "До хранения в бд:" + String.valueOf(bytec));
        Log.d(LOG_TAG, "После после хранения в бд:" + String.valueOf(bytec2));

Here is the log output:

04-16 20: 04: 27.220: D / myLogs (279): Before storage in the database: [B @ 44eb6f48 04-16 20: 04: 27.220: D / myLogs (279): After storage in the database: [B @ 44eb8ba0

With what it can be connected?


Your reasoning is false. The fact that you received different byte sequences does not mean that you have different data. Check the data yourself.

And what you output to the log is just the address of the object. And, of course, it can be different (and it is even logical that in this example it is different. The String.valueOf(bytec) displays the values ​​of the address as a variable, not the content.

Scroll to Top