Capturing SQL Error Messages (Embedded SQL Guide)

From InterBase
Jump to: navigation, search

Go Up to Additional InterBase Error Handling


Instead of displaying SQL error messages, an application can capture the text of those messages in a buffer by using isc_sql_interprete(). Capture messages in a buffer when applications:

  • Run under windowing systems that do not permit direct writing to the screen.
  • Store a record of all error messages in a log file.
  • Manipulate or format error messages for display.

Given SQLCODE, a pointer to a storage buffer, and the maximum size of the buffer in bytes, isc_sql_interprete() builds a SQL error message string, and puts the formatted string in the buffer where it can be manipulated. A buffer size of 256 bytes is large enough to hold any SQL error message.

For example, the following code stores a SQL error message in err_buf, then writes err_buf to a log file:

. . .
char err_buf[256]; /* error message buffer for isc_sql_interprete() */
FILE *efile=NULL; /* code fragment assumes pointer to an open file */
. . .
EXEC SQL
SELECT CITY INTO :city FROM STATES
WHERE STATE = :stat:statind;
if (SQLCODE)
{
isc_sql_interprete(SQLCODE, err_buf, sizeof(err_buf));
if(efile==NULL) efile=fopen("errors", "w");
fprintf(efile, "%s\n", err_buf); /* write buffer to log file */
EXEC SQL
ROLLBACK; /* undo database changes */
EXEC SQL
DISCONNECT ALL; /* close open databases */
exit(1); /* exit with error flag set */
}
. . .

isc_sql_interprete() retrieves and formats a single message corresponding to a given SQLCODE. When SQLCODE is less than –1, more specific InterBase error information is available. It, too, can be retrieved, formatted, and stored in a buffer by using the isc_interprete() function.