Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2004
    Posts
    9

    Red face Unanswered: ' ' used when insterting byte[] to BYTEA?

    Hi,

    because I can't try it from where I am for the next few days, I must post this question.

    When inserting a byte[] to a BYTEA field in PostgreSQL do I need to write it like 'byte[]' or ist byte[] enough??

    From Java:
    like this:
    String sql ="INSERT INTO person (picture) " + " VALUES ('" + imageToDBFormat(myPerson.getPicture()) + "');";

    or like this:
    String sql ="INSERT INTO person (picture) " + " VALUES (" + imageToDBFormat(myPerson.getPicture()) + ");";

    AND my other question would be, how do I tell Java that in the String there is also a byte[]. Or how do I tell it PostgreSQL anyway. Even in the commad line I have to write a string, so must I convert it to a string first?
    I read something about \\000, so can I just write
    '\\imageToDBFormat(myPerson.getPicture())'
    Is that the way you insert a byte[]?


    thx a lot
    Last edited by pir2004; 04-12-04 at 10:31.

  2. #2
    Join Date
    Apr 2004
    Posts
    9

    answer my own question :-)

    You must not use ' ' when saving a byte[] to a DB.

    For Java it is better to use a PreparedStatement with just one ? for the byte[]. Then you will have no problems.
    Just like this:

    PreparedStatement ps;
    ps = c.prepareStatement("INSERT INTO person (person_pk, firstname, lastname, picture)" + " VALUES ((nextval('incpkperson')), '" + myPerson.getFirstName() + "', '" + myPerson.getLastName() + "', ?);");
    ps.setBytes(1, imageToDBFormat(myPerson.getPicture()));
    ps.executeUpdate();
    ps.close();
    Last edited by pir2004; 04-12-04 at 17:50.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •