INSERT binary data

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gregor =?UTF-8?B?S292YcSN?=

    #1

    INSERT binary data

    Hi!

    I'm using JDBC to connect to DB2. I have a binary data (array of bytes) and
    want to save them.
    Is it possible to write an INSERT statement to do this:
    INSERT INTO TABLE (ID, BYTES) VALUES(1, '0x33 0x34') -- for example

    or do I have to use PreparedStateme nt and setBlob/setBytes method?

    Best regards,
    Kovi

    --
    _______________ _____________
    |http://kovica.blogspot .com|
    -----------------------------~-~-~-~-~-~-~-~-~-~-
    | In A World Without Fences Who Needs Gates? |
    | Experience Linux. |
    -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
  • Knut Stolze

    #2
    Re: INSERT binary data

    Gregor Kovač wrote:
    Hi!
    >
    I'm using JDBC to connect to DB2. I have a binary data (array of bytes)
    and want to save them.
    Is it possible to write an INSERT statement to do this:
    INSERT INTO TABLE (ID, BYTES) VALUES(1, '0x33 0x34') -- for example
    >
    or do I have to use PreparedStateme nt and setBlob/setBytes method?
    You can do this:

    INSERT INTO table(id, bytes) VALUES (1, x'3334')

    --
    Knut Stolze
    DB2 z/OS Utilities Development
    IBM Germany

    Comment

    • Gregor =?UTF-8?B?S292YcSN?=

      #3
      Re: INSERT binary data

      Knut Stolze wrote:
      Gregor Kovač wrote:
      >
      >Hi!
      >>
      >I'm using JDBC to connect to DB2. I have a binary data (array of bytes)
      >and want to save them.
      >Is it possible to write an INSERT statement to do this:
      >INSERT INTO TABLE (ID, BYTES) VALUES(1, '0x33 0x34') -- for example
      >>
      >or do I have to use PreparedStateme nt and setBlob/setBytes method?
      >
      You can do this:
      >
      INSERT INTO table(id, bytes) VALUES (1, x'3334')
      >
      I get SQL0408 : A value is not compatible with the data type of its
      assignment target. Target name is "bytes".
      for this one.
      The column bytes is defined like BLOB(1M) .

      Best regards,
      Kovi

      --
      _______________ _____________
      |http://kovica.blogspot .com|
      -----------------------------~-~-~-~-~-~-~-~-~-~-
      | In A World Without Fences Who Needs Gates? |
      | Experience Linux. |
      -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-

      Comment

      • Gregor =?UTF-8?B?S292YcSN?=

        #4
        Re: INSERT binary data

        Gregor Kovač wrote:
        Knut Stolze wrote:
        >
        >Gregor Kovač wrote:
        >>
        >>Hi!
        >>>
        >>I'm using JDBC to connect to DB2. I have a binary data (array of bytes)
        >>and want to save them.
        >>Is it possible to write an INSERT statement to do this:
        >>INSERT INTO TABLE (ID, BYTES) VALUES(1, '0x33 0x34') -- for example
        >>>
        >>or do I have to use PreparedStateme nt and setBlob/setBytes method?
        >>
        >You can do this:
        >>
        >INSERT INTO table(id, bytes) VALUES (1, x'3334')
        >>
        >
        I get SQL0408 : A value is not compatible with the data type of its
        assignment target. Target name is "bytes".
        for this one.
        The column bytes is defined like BLOB(1M) .
        >
        Best regards,
        Kovi
        >
        Tis works if the column is defined like VARCHAR(2000) FOR BIT DATA, for
        exmaple.
        What to do for BLOB columns ?

        Best regars,
        Kovi

        --
        _______________ _____________
        |http://kovica.blogspot .com|
        -----------------------------~-~-~-~-~-~-~-~-~-~-
        | In A World Without Fences Who Needs Gates? |
        | Experience Linux. |
        -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-

        Comment

        • Knut Stolze

          #5
          Re: INSERT binary data

          Gregor Kovač wrote:
          Gregor Kovač wrote:
          >
          >Knut Stolze wrote:
          >>
          >>Gregor Kovač wrote:
          >>>
          >>>Hi!
          >>>>
          >>>I'm using JDBC to connect to DB2. I have a binary data (array of bytes)
          >>>and want to save them.
          >>>Is it possible to write an INSERT statement to do this:
          >>>INSERT INTO TABLE (ID, BYTES) VALUES(1, '0x33 0x34') -- for example
          >>>>
          >>>or do I have to use PreparedStateme nt and setBlob/setBytes method?
          >>>
          >>You can do this:
          >>>
          >>INSERT INTO table(id, bytes) VALUES (1, x'3334')
          >>>
          >>
          >I get SQL0408 : A value is not compatible with the data type of its
          >assignment target. Target name is "bytes".
          >for this one.
          >The column bytes is defined like BLOB(1M) .
          >
          Tis works if the column is defined like VARCHAR(2000) FOR BIT DATA, for
          exmaple.
          What to do for BLOB columns ?
          I just use prepared statements for those.

          But you should still be able to convert the binary data to its hex
          representation and then use the above INSERT statement as is. However, you
          have to be aware that the hex representation is a string literal to the SQL
          compiler and must not exceed the length limits.

          --
          Knut Stolze
          DB2 z/OS Utilities Development
          IBM Germany

          Comment

          • 4.spam@mail.ru

            #6
            Re: INSERT binary data

            >
            Tis works if the column is defined like VARCHAR(2000) FOR BIT DATA, for
            exmaple.
            What to do for BLOB columns ?
            >
            INSERT INTO table(id, bytes) VALUES (1, blob(x'3334') )

            Comment

            • Gregor =?UTF-8?B?S292YcSN?=

              #7
              Re: INSERT binary data

              4.spam@mail.ru wrote:
              >>
              >Tis works if the column is defined like VARCHAR(2000) FOR BIT DATA, for
              >exmaple.
              >What to do for BLOB columns ?
              >>
              >
              INSERT INTO table(id, bytes) VALUES (1, blob(x'3334') )
              Thank you both. :))
              This is what I've been looking for.

              --
              _______________ _____________
              |http://kovica.blogspot .com|
              -----------------------------~-~-~-~-~-~-~-~-~-~-
              | In A World Without Fences Who Needs Gates? |
              | Experience Linux. |
              -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-

              Comment

              Working...