2

I want to store a long string into sqlite of android. I write it into sqlite by:

String content = bodytext.toString();
    byte[] contentByte = content.getBytes();
    Log.d("bytetoString",new String(contentByte));
    String sql1 = "insert into content(id,content) values( '" + messageID
            + "','" + contentByte + "');";
    db.execSQL(sql1); 

and I read it by:

String sql = "select * from content where id='" + messageID + "'";
    Cursor temp = db.rawQuery(sql, null);
    if (temp.moveToNext()) {
        byte[] contentByte = temp.getBlob(temp.getColumnIndex("content"));
        String sb = new String(contentByte);
        Log.d("String",sb);
        temp.close();
        return sb;
    }

However, when I print the string which I just read. it prints out something like " [B@41431930?? " is there something wrong?

Ryan.ye
  • 39
  • 1
  • 7

1 Answers1

3

It is an array and toString() prints just memory address. That is why you are seeing [B@41431930??

If you want readable values use something like java.util.Arrays.toString(contentByte)

kosa
  • 65,990
  • 13
  • 130
  • 167
  • thanks for your help. but there's still problem. I try java.util.Arrays.toString(contentByte) but it returns "[91,66,64,52...]" , I wonder if there are some error when I write the bytes to the sqlite? – Ryan.ye Aug 10 '12 at 02:35
  • see if this discussion helps http://stackoverflow.com/questions/6684665/java-byte-array-to-string-to-byte-array – kosa Aug 10 '12 at 02:41
  • 2
    I find the bug now. I had store the address of the contentByte into the SQLite by using "insert into content(id,content) values( '" + messageID+ "','" + contentByte + "');" , this code convert contentByte to String by calling contentByte.toString(). So I change it to "db.execSQL("insert into content(id,content) values(?,?)",new Object[]{messageID,contentByte}); ", and then, it works. Thanks a lot!! – Ryan.ye Aug 10 '12 at 03:01
  • Thanks Ryan, this approach (db.execSQL("insert into content(id,content) values(?,?)",new Object[]{messageID,contentByte}); ) is working fine for me. Instead of comment you can create a separate answer you will get up votes – Bikesh M May 09 '17 at 14:39