Using ct_get_data to fetch text and image values

Only columns that follow the last column bound with ct_bind are available for use with ct_get_data.

For example, if an application selects four columns, all of which are text, and binds the first and third columns to program variables, then the application cannot use ct_get_data to retrieve the text contained in the second column. However, it can use ct_get_data to retrieve the text in the fourth column. Applications that control the select statement can reorder the select list so that the text and image columns come at the end.

To retrieve a text or image value using ct_get_data, an application follows these steps:

  1. Executes a command that generates a result set that contains text or image columns.

    An application uses a language command, RPC command, or dynamic SQL command to generate a result set containing text or image columns.

    For example, the pic column in the au_pix table of the pubs2 database contains authors’ pictures. To retrieve them, an application might execute the following language command:

    ct_command(cmd, CS_LANG_CMD, 
    
             "select pic from au_pix”,
    
             CS_NULLTERM, CS_UNUSED);
    
     ct_send(cmd);
    
  2. Processes the result set containing the text or image column.

    An application uses ct_fetch to loop through the rows contained in the result set. Inside the loop, for each unbound text or image column:

    • The application calls ct_get_data in a loop to retrieve the text or image data for the column.

    • The application calls ct_get_info to get an I/O descriptor that updates the column at a later time.

    Most applications use a program structure similar to the following:

    while ct_fetch is returning rows
    
         process any bound columns
    
         for each unbound text or image column
    
             while ct_get_data is returning data
    
                 process the data
    
             end while
    
             ct_data_info to get the column’s CS_IODESC
    
         end for
    
     end while
    

    Alternatively, for each unbound text or image column, an application:

    • Calls ct_get_data with the parameter buflen as 0, so that it returns no data but does refresh the I/O descriptor for the column.

    • Calls ct_get_data to get the I/O descriptor for the column. The total_txtlen field in this structure represents the total length of the text or image value.

    • Calls ct_get_data as many times as necessary to retrieve the value.

    This method has the advantage of allowing an application to determine the total length of a text or image value before retrieving it.