[ Pobierz całość w formacie PDF ]
.} /* end of switch statement */if (sqltype & 1){/* allocate variable to hold NULL status */98 INTERBASE 5DSQL PROGRAMMING METHODSvar->sqlind = (short *)malloc(sizeof(short));}} /* end of for loop */For more information about datatype coercion and NULL indicators, see Coercingdatatypes on page 92.7.Execute the named statement string with isc_dsql_execute().For example, thefollowing statement executes a statement string named stmt:isc_dsql_execute(status_vector, &trans, &stmt, 1, in_sqlda);Re-executing the statement stringOnce a non-query statement string with parameters is prepared, it can be executed asoften as required in an application.Before each subsequent execution, the input XSQLDAcan be supplied with new parameter and NULL indicator data.To supply new parameter and NULL indicator data for a prepared statement, repeat step6 of Preparing and executing a statement string with parameters on page 96.Method 3: Query statements without parametersThere are three steps to processing an SQL query statement string without parameters:1.Prepare an output XSQLDA to process the select-list items returned when thequery is executed.2.Prepare the statement string.3.Use a cursor to execute the statement and retrieve select-list items from theoutput XSQLDA.Preparing the output XSQLDAMost queries return one or more rows of data, referred to as a select-list.Because thenumber and kind of items returned are unknown when a statement string is created, anoutput XSQLDA must be created to store select-list items that are returned at runtime.Toprepare the XSQLDA, follow these steps:1.Declare a variable to hold the XSQLDA needed to store the column data foreach row that will be fetched.For example, the following declaration createsan XSQLDA called out_sqlda:XSQLDA *out_sqlda;API GUIDE 99CHAPTER 6 WORKING WITH DYNAMIC SQL2.Optionally declare a variable for accessing the XSQLVAR structure of theXSQLDA:XSQLVAR *var;Declaring a pointer to the XSQLVAR structure is not necessary, but can simplifyreferencing the structure in subsequent statements.3.Allocate memory for the XSQLDA using the XSQLDA_LENGTH macro.Thefollowing statement allocates storage for out_sqlda:out_sqlda = (XSQLDA *)malloc(XSQLDA_LENGTH(10));Space for 10 XSQLVAR structures is allocated in this statement, enabling the XSQLDA toaccommodate up to 10 select-list items.4.Set the version field of the XSQLDA to SQLDA_VERSION1, and set the sqln fieldof the XSQLDA to indicate the number of XSQLVAR structures allocated:out_sqlda->version = SQLDA_VERSION1;out_sqlda->sqln = 10;Preparing a query statement string without parametersAfter an XSQLDA is created for holding the items returned by a query statement string, thestatement string can be created, prepared, and described.When a statement string isexecuted, InterBase creates the select-list of selected rows.To prepare a query statement string, follow these steps:1.Elicit a statement string from the user or create one that contains the SQLstatement to be processed.For example, the following statement creates anSQL statement string that performs a query:char *str = "SELECT * FROM CUSTOMER";The statement appears to have only one select-list item (*).The asterisk is a wildcardsymbol that stands for all of the columns in the table, so the actual number of itemsreturned equals the number of columns in the table.2.Declare and initialize an SQL statement handle, then allocate it withisc_dsql_allocate():isc_stmt_handle stmt; /* Declare a statement handle.*/stmt = NULL; /* Set handle to NULL before allocation.*/.isc_dsql_allocate_statement(status_vector, &db1, &stmt);100 INTERBASE 5DSQL PROGRAMMING METHODS3.Parse the statement string with isc_dsql_prepare().This sets the statementhandle (stmt) to refer to the parsed format.The statement handle is used insubsequent calls to statements such as isc_dsql_describe() and isc_dsql_execute():isc_dsql_prepare(status_vector, &trans, &stmt, 0, str, 1, NULL);4.Use isc_dsql_describe() to fill the output XSQLDA with information about theselect-list items returned by the statement:isc_dsql_describe(status_vector, &trans, &stmt, out_sqlda);5.Compare the sqln field of the XSQLDA to the sqld field to determine if theoutput descriptor can accommodate the number of select-list items specifiedin the statement
[ Pobierz całość w formacie PDF ]