This example demonstrates #define OTL_EXTENDED_EXCEPTION. See also problem 47, and the otl_exception's extended fields.
#include <iostream>
using namespace std;
#include <stdio.h>
#define OTL_ODBC_MSSQL_2005 // Compile OTL 4/ODBC, MS SQL 2005
#define OTL_FREETDS_ODBC_WORKAROUNDS // Enable the FreeTDS / ODBC workarounds for MS SQL
//#define OTL_ODBC // Compile OTL 4/ODBC. Uncomment this when used with MS SQL 7.0/ 2000
//#define OTL_ODBC_UNIX // Compile OTL 4 / ODBC. Uncomment this when used in Linux / Unix
#define OTL_EXTENDED_EXCEPTION // Enable extended field in otl_exception
#include <otlv4.h> // include the OTL 4.0 header file
otl_connect db; // connect object
void insert()
// insert rows into table
{
otl_stream o(10, // buffer size
"insert into test_tab values(:f1<int>,:f2<char[31]>",
// SQL statement
db // connect object
);
o<<100<<"Row 100";
}
int main()
{
otl_connect::otl_initialize(); // initialize ODBC environment
try{
db.rlogon("scott/tiger@freetds_mssql"); // connect to MS SQL Server
otl_cursor::direct_exec
(
db,
"drop table test_tab",
otl_exception::disabled // disable OTL exceptions
); // drop table
otl_cursor::direct_exec
(
db,
"create table test_tab(f1 int, f2 varchar(30))"
); // create table
insert(); // insert records into table
}
catch(otl_exception& p){ // intercept OTL exceptions
cerr<<p.msg<<endl; // print out error message
cerr<<p.stm_text<<endl; // print out SQL that caused the error
cerr<<p.sqlstate<<endl; // print out SQLSTATE info.
cerr<<p.var_info<<endl; // print out the variable that caused the error
if(p.arr_len>0){ // checking if the extended fields were populated
for(int j=0;j<p.arr_len;++j){
cout<<"MSG["<<j<<"]="<<p.msg_arr[j]<<endl; // message array
cout<<"SQLSTATE["<<j<<"]="<<p.sqlstate_arr[j]<<endl; // sqlstate array
cout<<"CODE["<<j<<"]="<<p.code_arr[j]<<endl; // code array
}
}
}
db.logoff(); // disconnect from MS SQL Server
return 0;
}
insert into test_tab values(? ,?
42000
MSG[0]=[FreeTDS][SQL Server]Statement(s) could not be prepared.
SQLSTATE[0]=42000
CODE[0]=8180
MSG[1]=[FreeTDS][SQL Server]Incorrect syntax near '@P2'.
SQLSTATE[1]=42000
CODE[1]=102
Copyright © 1996, 2008, Sergei Kuchin, email: skuchin@aceweb.com, skuchin@gmail.com.
Permission to use, copy, modify and redistribute this document for any purpose is hereby granted without fee, provided that the above copyright notice appear in all copies.