Home | Previous Page | Next Page   General SQL API Features (ESQL/C) > Handling Locale-Specific Data >

Avoiding Partial Characters

When you use a locale that supports a multibyte code set, make sure that you define buffers large enough to avoid the generation of partial characters. Possible areas for consideration are as follows:

For more detailed examples of partial characters, see Partial Characters in Column Substrings.

Copying Character Data

When you copy data, you must ensure that the buffers are an adequate size to hold the data. If the destination buffer is not large enough for the multibyte data in the source buffer, the data might be truncated during the copy.

For example, the following ESQL/C code fragment copies the multibyte data A1A2A3B1B2B3 from buf1 to buf2:

char buf1[20], buf2[5];
...
stcopy("A1A2A3B1B2B3", buf1);
...
stcopy(buf1, buf2);

Because buf2 is not large enough to hold the multibyte string, the copy truncates the string to A1A2A3B1B2. To prevent this situation, ensure that the multibyte string fits into a buffer before the ESQL/C program performs the copy.

Using Code-Set Conversion

If you have a character buffer to hold character data from a database, you must ensure that this buffer is large enough to accommodate any expansion that might occur if the application uses code-set conversion. If the client and database locales are different and convertible, the application might need to expand this value. For more information, see Performing Code-Set Conversion.

For example, if the fname column is defined as CHAR(8), the following ESQL/C code fragment selects an 8-byte character value into the 10-byte buf1 host variable:

char buf1[10];
...
EXEC SQL select fname into :buf1 from tab1 
         where cust_num = 29;

You might expect a 10-byte buffer to be adequate to hold an 8-byte character value from the database. If the client application expands this value to 12 bytes, however, the value no longer fits in the buf1 buffer. The fname value is truncated to fit in buf1, possibly creating partial characters if fname contains multibyte characters. For more information, see Partial Characters in Column Substrings.

To avoid this situation, define buffers to handle the maximum character-expansion possible, 4 bytes, in the conversion between your client and database code sets.

Home | [ Top of Page | Previous Page | Next Page | Contents | Index ]