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?

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

4 Comments

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?
see if this discussion helps stackoverflow.com/questions/6684665/…
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!!
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.