Preparing the Input XSQLDA
Go Up to Method 4: Query Statements With Parameters
Placeholder parameters are replaced with actual data before a prepared SQL statement string is executed. Because those parameters are unknown when the statement string is created, an input XSQLDA must be created to supply parameter values at runtime. To prepare the XSQLDA, follow these steps:
- Declare a variable to hold the
XSQLDAneeded to process parameters. For example, the following declaration creates anXSQLDAcalledin_sqlda:XSQLDA *in_sqlda;
- Optionally declare a variable for accessing the
XSQLVARstructure of theXSQLDA:XSQLVAR *var;
Declaring a pointer to theXSQLVARstructure is not necessary, but can simplify referencing the structure in subsequent statements. - Allocate memory for the
XSQLDAusing theXSQLDA_LENGTHmacro. The following statement allocates storage forin_slqda:in_sqlda = (XSQLDA *)malloc(XSQLDA_LENGTH(10));
In this statement, space for tenXSQLVARstructures is allocated, allowing theXSQLDAto accommodate up to ten input parameters. Once structures are allocated, assign values to thesqldatafields. - Set the
versionfield of theXSQLDAtoSQLDA_CURRENT_VERSION, and set thesqlnfield of theXSQLDAto indicate the number ofXSQLVARstructures allocated:in_sqlda->version = SQLDA_CURRENT_VERSION; in_sqlda->sqln = 10;