access varchar error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jackyshi3333@yahoo.com.cn

    access varchar error

    In my application,I use SQLBindCol to retrive the information, however
    I found that in one table there is a varchar(10) column,
    if the buffer size specified in the SQLBindCol is also ten,and the
    buffer is ten length, then the result will be turned into 9.If the
    buffer size specified as 11, then the result will be ok.
    Does anyone explane for this?

  • Knut Stolze

    #2
    Re: access varchar error

    jackyshi3333@ya hoo.com.cn wrote:
    [color=blue]
    > In my application,I use SQLBindCol to retrive the information, however
    > I found that in one table there is a varchar(10) column,
    > if the buffer size specified in the SQLBindCol is also ten,and the
    > buffer is ten length, then the result will be turned into 9.If the
    > buffer size specified as 11, then the result will be ok.
    > Does anyone explane for this?[/color]

    The issue lies in the handling of strings in the C programming language. A
    string is terminated by a trailing '\0' (= 0x00) byte to denote the end.
    If you provide a buffer of 10 bytes, then only 9 bytes are available for
    the actual data and the last byte is needed for the termination character.
    That's a standard C/C++ thing, which needs to be solved by using buffers
    that have (at least) one more byte than is needed for the real data.

    --
    Knut Stolze
    DB2 Information Integration Development
    IBM Germany

    Comment

    • jacky shi

      #3
      Re: access varchar error


      Knut Stolze wrote:
      [color=blue]
      > The issue lies in the handling of strings in the C programming language. A
      > string is terminated by a trailing '\0' (= 0x00) byte to denote the end.
      > If you provide a buffer of 10 bytes, then only 9 bytes are available for
      > the actual data and the last byte is needed for the termination character.
      > That's a standard C/C++ thing, which needs to be solved by using buffers
      > that have (at least) one more byte than is needed for the real data.
      >
      > --
      > Knut Stolze
      > DB2 Information Integration Development
      > IBM Germany[/color]

      sorry,I don't think so.In fact I have considered that.In my
      application,I allocate 11 bypes but I specify the length as 10 in the
      function then the problem happen. And for the oracle, almost the same
      function works well.
      Does this mean I have to specify the parameter one more than the length
      in the database?

      Comment

      • Knut Stolze

        #4
        Re: access varchar error

        jacky shi wrote:
        [color=blue]
        >
        > Knut Stolze wrote:
        >[color=green]
        >> The issue lies in the handling of strings in the C programming language.
        >> A string is terminated by a trailing '\0' (= 0x00) byte to denote the
        >> end. If you provide a buffer of 10 bytes, then only 9 bytes are available
        >> for the actual data and the last byte is needed for the termination
        >> character. That's a standard C/C++ thing, which needs to be solved by
        >> using buffers that have (at least) one more byte than is needed for the
        >> real data.
        >>
        >> --
        >> Knut Stolze
        >> DB2 Information Integration Development
        >> IBM Germany[/color]
        >
        > sorry,I don't think so.In fact I have considered that.In my
        > application,I allocate 11 bypes but I specify the length as 10 in the
        > function then the problem happen.[/color]

        How should DB2 know how much space you allocated unless you tell it so? You
        say that there are 10 bytes, so DB2 has 9 bytes for data and 1 byte for the
        termination character.
        [color=blue]
        > And for the oracle, almost the same
        > function works well.
        > Does this mean I have to specify the parameter one more than the length
        > in the database?[/color]

        Yes, the documentation for SQLBindCol() says so:

        Note that for character data, the driver _counts_ the NULL termination
        character and so space must be allocated for it.

        --
        Knut Stolze
        DB2 Information Integration Development
        IBM Germany

        Comment

        • jacky shi

          #5
          Re: access varchar error

          Thank you very much. I have read the document of the api, however,
          there isn't any thing refering that. Would you please tell me which
          documentation you refered?

          Comment

          • Jan M. Nelken

            #6
            Re: access varchar error

            jacky shi wrote:[color=blue]
            > Thank you very much. I have read the document of the api, however,
            > there isn't any thing refering that. Would you please tell me which
            > documentation you refered?
            >[/color]

            Did you try searchable online documentation available at:



            Specifically, for SQLBindCol() call you will find:

            "Note that for character data, the driver counts the NULL termination character
            and so space must be allocated for it."


            Jan M. Nelken

            Comment

            Working...