Collection Contents Index ODBC fundamentals Using prepared statements in ODBC pdf/chap4.pdf

Programming Interfaces Guide
   CHAPTER 4. ODBC Programming     

Error checking in ODBC


The previous example did not check for any errors. Errors in ODBC are reported using the return value from each of the ODBC API function calls and the SQLError function.

Every ODBC API function returns a RETCODE, which is one of the following status codes:

Every environment, connection, and statement handle can have one or more errors or warnings associated with it. Each call to SQLError returns the information for one error and removes the information for that error. If you do not call SQLError to remove all errors, the errors are removed on the next function call that passes the same handle as a parameter.

Example 

The following program fragment uses SQLError and return codes:

HDBC dbc;
HSTMT stmt;
RETCODE retcode;
UCHAR errmsg[100];

. . .

retcode = SQLAllocStmt( dbc, &stmt );
if( retcode == SQL_ERROR ) {
   SQLError( env, dbc, SQL_NULL_HSTMT, NULL, NULL,
            errmsg, sizeof(errmsg), NULL );

   /* Assume that print_error is defined */
   print_error( "Failed SQLAllocStmt", errmsg );
   return;
}

/* Delete items for order 2015 */
retcode = SQLExecDirect( stmt,
      "delete from sales_order_items
      where id=2015", SQL_NTS );
if( retcode == SQL_ERROR ) {
   SQLError( env, dbc, stmt, NULL, NULL,
            errmsg, sizeof(errmsg), NULL );
   /* Assume that print_error is defined */
   print_error( "Failed to delete items", errmsg );
   return;
}
. . .

Note that each call to SQLError passes three handles for an environment, connection, and statement. The first call uses SQL_NULL_HSTMT to get the error associated with a connection. Similarly, a call with both SQL_NULL_DBC and SQL_NULL_HSTMT will get any error associated with the environment handle.

The return value from SQLError may seem confusing. It returns SQL_SUCCESS if there is an error to report (not SQL_ERROR), and SQL_NO_DATA_FOUND if there are no more errors to report.

The examples pass the null pointer for some of the parameters to SQLError. The help file contains a full description of SQLError and all its parameters.


Collection Contents Index ODBC fundamentals Using prepared statements in ODBC pdf/chap4.pdf