Re: C datatypes VS Oracle datatypes

From: C. Fetters <cfetters_at_iac.net>
Date: 1996/02/19
Message-ID: <3128F051.57C8_at_iac.net>#1/1


Tomas Zakrzewski wrote:
> I am programming in C using PRO*C.
> I have problem to handle data retrieved from Oracle.
> Example:
> datatype in Oracle of the field is varchar2[5].
>
> Retrieved data from Oracle are 5 characters long therefore they takes the
> whole space of the array. If I declare array size of 6 bytes then I can manipulate array with C library functions, but I can not update or insert back to Oracl
> that array. If I declare array of five bytes there is no problem with Oracle, but
> the string in C is not terminated by NULL.
>
> How this problem is handled. Do I have to maintain two copies of each string in case I want to
> update the Oracle. (one of the same size and second 1 byte larger for NULL use in C).
>

I always hated using the varchar2 datatype within a C program. It got ugly setting the length field all of the time and converting back and forth between C strings and varchar2's. There is a section somewhere in the Pro*C manual about datatype equivalencing. The code looks something like this:

     char data[6]; exec sql var data is string(6);

From then on, you can use the variable "data" in normal string operations and use it in SQL statements. Oracle knows the data is NULL terminated if you're using it for inserts/updates and it NULL terminates the data if you select into the variable.

Warning: the size specified for data (inside the []) must be EXACTLY the same as the size after the word "string". That's why I like to put both statements on one line. If I change the size of data, I have a better chance of remembering to change the string size if it's on the same line. Also note that I used a 6 in my example for use with you varchar2 field that is 5 chars long. Received on Mon Feb 19 1996 - 00:00:00 CET

Original text of this message