Select into an lvarchar pointer host variable

The following example code illustrates the steps to select data from an opaque type column into an lvarchar pointer host variable. To simplify the example, the code does not check returned values for errors.
#include <stdio.h>
exec sql include “polyvar.h” /* includes udt - polygon_type */

main()
{
   exec sql begin declare section;
      lvarchar ‘polygon_type’ *mypoly1
      char *buffer;
      int size, p_id, len;
   exec sql end declare section;

   ifx_var_flag(&mypoly1, 1); /* ESQL/C run time will do the allocation 
                               */

   exec sql select poly into :mypoly1 from polygon_tab where p_id = 1;
   if ( ifx_var_getlen(&mypoly1) > 0 ) { /* If select returns valid data 
                                         */
   buffer = (char *)ifx_var_getdata(&mypoly1); /*Access data in 
                                                * mypoly1*/
   printf(“Length of data : %ld \n”, (int)ifx_var_getlen(&mypoly) );
   printf("Data: %s \n", buffer);
   ifx_var_dealloc(&mypoly1); /* Always users responsibility to free */
}
The example code performs the following steps:
  1. It declares the lvarchar pointer host variable, *mypoly1.
  2. It calls the ifx_var_flag() function to specify that it will let IBM® Informix® ESQL/C allocate memory for the data buffer (flag equals 1). Informix ESQL/C allocates the memory by default if you do not call ifx_var_flag().
  3. It selects the column poly into the *mypoly host variable.
  4. It calls ifx_var_getdata() to obtain the address of the data buffer, casting the return value to char * and storing the address in buffer.
  5. It calls ifx_var_getlen() to illustrate how to obtain the length of the data that was retrieved.
  6. It deallocates the memory that Informix ESQL/C allocated for *mypoly1.

For an example that uses lvarchar pointers as host variables for selecting from collection columns, see The lvarptr.ec program.


Copyright© 2019 HCL Technologies Limited