How to use the NCHAR (unicode) data type for host variables in ESQL/C?

~ 0 min
2016-03-08 08:00

Please check esqltype.h has defined NCHAR like below:

typedef char    nchar;             /* for unicode #004 */ typedef varchar nvarchar;          /* for unicode #004 */ typedef longvarchar nclob;         /* for unicode #004 */ 

If esqltype.h has this define, user can use nchar in ESLQ/C, but user should put correct unicode (transfer local code to unicode by themselves) data into nchar buffer.

For example:

create table nt1 (c1 int, c2 nchar(10));

 /****************************************************************************
 * test nchar: test nchar

 * NOTE: user must make sure their input unicode data is correct, we do not provide unicode <-> local utility function

 **********************************************************
test_nchar()
{
   EXEC SQL begin declare section;
   nchar tc2[22];
   nchar hvstring[300];
   char buf[300];
   int tc1, ind1, ind2;
   EXEC SQL end declare section;
   int i;

 

   printf("\ntest nchar: -----\n");

 

   $ delete from nt1;
     
   // this is a local string
   sprintf(buf, "insert into nt1 values (1, '1234567890')");
   // The following is a lazy way to convert ascii string
   for (i = 0; i < strlen(buf); i++)
      {
      hvstring[i*2] = buf[i];
      hvstring[i*2+1] = 0;
      }
   // Add NULL terminate 0x0000 for unicode string
   hvstring[i*2] = hvstring[i*2+1] = 0;

 

   // execute the unicode sql command
   $ execute immediate from :hvstring;
   chkErr();
  
   // select the result to a unicode string buffer
   $ select * from nt1 into :tc1 :ind1, :tc2 :ind2;
   chkErr();
  
   printf("tc1=%d, ind2=%d, tc2=", tc1, ind2);
  
   // print out the hex for the unicode string
   for (i = 0; i < ind2; i++)
      printf("%02x", (unsigned char)tc2[i]);
     
   $ delete from nt1;
   chkErr();

 

   // insert the unicode string data to table  
   $ insert into nt1 values (:tc1 :ind1, :tc2 :ind2);
   chkErr();
  
   printf("\n");

 

   $ commit;
   chkErr();
}

 

Average rating 0 (0 Votes)

You cannot comment on this entry

Tags