DB2 Version 9.7 for Linux, UNIX, and Windows

Host variables in C and C++ embedded SQL applications

Host variables are C or C++ language variables that are referenced within SQL statements. They allow an application to exchange data with the database manager. After the application is precompiled, host variables are used by the compiler as any other C or C++ variable. Follow the rules described in the following sections when naming, declaring, and using host variables.

Long variable considerations

In applications that manually construct the SQLDA, long variables cannot be used when sqlvar::sqltype==SQL_TYP_INTEGER. Instead, sqlint32 types must be used. This problem is identical to using long variables in host variable declarations, except that with a manually constructed SQLDA, the precompiler will not uncover this error and run time errors will occur.

Any long and unsigned long casts that are used to access sqlvar::sqldata information must be changed to sqlint32 and sqluint32 respectively. Val members for the sqloptions and sqla_option structures are declared as sqluintptr. Therefore, assignment of pointer members into sqla_option::val or sqloptions::val members should use sqluintptr casts rather than unsigned long casts. This change will not cause run-time problems in 64-bit UNIX and Linux operating systems, but should be made in preparation for 64-bit Windows applications, where the long type is only 32-bit.

Multi-byte encoding considerations

Some character encoding schemes, particularly those from east-Asian regions, require multiple bytes to represent a character. This external representation of data is called the multi-byte character code representation of a character, and includes double-byte characters (characters represented by two bytes). Host variables will be chosen accordingly since graphic data in DB2® consists of double-byte characters.

To manipulate character strings with double-byte characters, it may be convenient for an application to use an internal representation of data. This internal representation is called the wide-character code representation of the double-byte characters, and is the format customarily used in the wchar_t C or C++ data type. Subroutines that conform to ANSI C and X/OPEN Portability Guide 4 (XPG4) are available to process wide-character data, and to convert data in wide-character format to and from multi-byte format.

Note that although an application can process character data in either multi-byte format or wide-character format, interaction with the database manager is done with DBCS (multi-byte) character codes only. That is, data is stored in and retrieved from GRAPHIC columns in DBCS format. The WCHARTYPE precompiler option is provided to allow application data in wide-character format to be converted to/from multi-byte format when it is exchanged with the database engine.