ODBC error handling

Source: Internet
Author: User
Tags error handling odbc sybase

Error handling in ODBC

The error in ODBC is reported using the return value from each ODBC function call and the return value of the SQLError function or SQLGetDiagRec function. The SQLError function is used for versions prior to ODBC version 3 (but not version 3). It is not recommended to use the SQLError function since version 3, this function has been replaced by the SQLGetDiagRec function.

Each ODBC function returns a Sqlreturn, which is one of the following status codes:

status code description
sql_success no error.
sql_success_with_info

The function is complete, but a call to SQLError will display a warning.

The most common scenario for this state is that the returned value is too long and the buffer provided by the application is not sufficient.

sql_error function was not completed because an error occurred. Call SQLError to get more information about this issue.
sql_invalid_handle

is not valid for an environment, connection, or statement handle passed as a parameter. This typically occurs if the handle is not used after the handle is disposed, or if the handle is a null-value pointer,

.

sql_no_data_found

No information available. The most common case where

uses this state is when reading from a cursor, which indicates that there are no more rows in the cursor.

sql_need_data

parameter requires data.

This is an advanced feature that is described in the following ODBC SDK documentation for SQLParamData and SQLPutData.

Each environment, connection, and statement handle may have one or more errors or warnings associated with it. Each call to SQLError or SQLGetDiagRec returns information about an error, and then deletes the information about the error. If you do not call SQLError or SQLGetDiagRec to remove all errors, these errors are removed when you execute the next function call that passes the same handle as a parameter.

Each call to SQLError passes three handles, each of which is used for the environment, joins, and statements. The first call uses SQL_NULL_HSTMT to get the error associated with the connection. Similarly, calls that use SQL_NULL_DBC and sql_null_hstmt will get any errors associated with the environment handle.

Each call to SQLGetDiagRec passes the environment, connection, or statement handle. The first call passes a handle type SQL_HANDLE_DBC to get the error associated with the connection. The second call passes the handle type sql_handle_stmt to get the error associated with the statement that was just executed.

If there are errors to report ( not sql_error), SQLError and SQLGetDiagRec will return sql_success, and if there are no other errors to report, then Sql_no_data_found will be returned.

Example 1

The following code snippet uses SQLError and returns the code:



/* Declare required variables */sqlhdbc DBC; SQLHSTMT stmt; Sqlreturn Retcode; UCHAR errmsg[100];/* Code omitted here */retcode = Sqlallochandle (sql_handle_stmt, 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 ("Allocation failed", errmsg);   return;} /* Delete items for order */retcode = SQLExecDirect (stmt,       "Delete from Salesorderitems WHERE id=2015",       Sql_n TS); 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;}
Example 2

The following code snippet uses SQLGetDiagRec and returns the code:



/* Declare required variables */sqlhdbc DBC; SQLHSTMT stmt; Sqlreturn Retcode; Sqlsmallint Errmsglen; Sqlinteger errnative; UCHAR errmsg[255]; UCHAR Errstate[5]; /* Code omitted here */retcode = Sqlallochandle (sql_handle_stmt, DBC, &stmt); if (Retcode = = sql_error) {   sqlgetdi Agrec (SQL_HANDLE_DBC, DBC, 1, errstate,       &errnative, errmsg, sizeof (errmsg), &errmsglen);    /* Assume that print_error is defined *   /print_error ("Allocation failed", Errstate, Errnative, errmsg);   return;} /* Delete items for order */retcode = SQLExecDirect (stmt,       "Delete from Salesorderitems WHERE id=2015",       Sql_n TS); if (Retcode = = sql_error) {   SQLGetDiagRec (sql_handle_stmt, STMT,        recnum, Errstate,       &errnative, errmsg , sizeof (ERRMSG), &errmsglen);   /* Assume that print_error is defined *   /print_error ("Failed to delete items",        errstate, errnative, errmsg); 
      return;}

ODBC error handling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.