Using isc_dsql_prepare() and isc_dsql_execute()

From InterBase
Jump to: navigation, search

Go Up to Method 1: Non-query Statements Without Parameters


To execute a statement string several times, use isc_dsql_allocate_statement(), isc_dsql_prepare(), and isc_dsql_execute():

  1. Elicit a statement string from the user or create one that contains the SQL statement to be processed. For example, the following statement creates a SQL statement string:
    char *str = "UPDATE DEPARTMENT SET BUDGET = BUDGET * 1.05";
    
  2. Declare and initialize a SQL statement handle, then allocate it with isc_dsql_allocate_statement():
    isc_stmt_handle stmt; /* Declare a statement handle. */
    stmt = NULL; /* Set handle to NULL before allocation. */
    . . .
    isc_dsql_allocate_statement(status_vector, &db1, &stmt);
    
  3. Parse the statement string with isc_dsql_prepare(). This sets the statement handle (stmt) to refer to the parsed format. The statement handle is used in subsequent calls to isc_dsql_execute():
    isc_dsql_prepare(status_vector, &trans, &stmt, 0, str, 1, NULL);
    
    isc_dsql_prepare() also accepts string literals. For example,
    isc_dsql_prepare(status_vector, &trans, &stmt, 0,
    "UPDATE DEPARTMENT SET BUDGET = BUDGET * 1.05", 1, NULL);
    
  4. Execute the named statement string using isc_dsql_execute(). For example, the following statement executes a statement string named stmt:
    isc_dsql_execute(status_vector, &trans, &stmt, 1, NULL);
    
    Once a statement string is prepared, it can be executed as many times as required in an application.