在ESQL/C里怎样为主变量使用NCHAR (unicode)数据类型?
请如下所示检查esqltype.h定义的NCHAR。
typedef char nchar; /* for unicode #004 */
typedef varchar nvarchar; /* for unicode #004 */
typedef longvarchar nclob; /* for unicode #004 */
如果esqltype.h有这种定义,用户能够在ESLQ/C使用nchar,但是用户应该给出正确的Unicode(自行将本地字符转换为Unicode)数据到nchar缓冲器。
例如:
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();
}