This example demonstrates #define OTL_EXTENDED_EXCEPTION. See also problem 47, and the otl_exception's extended fields.
#include <iostream.h> #include <stdio.h>
#define OTL_ODBC // Compile OTL 4.0/ODBC
#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@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;
}
[Microsoft][ODBC SQL Server Driver][SQL Server]Line 1: Incorrect syntax near '@P2'. insert into test_tab values(? ,? 42000 MSG[0]=[Microsoft][ODBC SQL Server Driver][SQL Server]Line 1: Incorrect syntax near '@P2'. SQLSTATE[0]=42000 CODE[0]=170 MSG[1]=[Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepared. SQLSTATE[1]=42000 CODE[1]=8180
Copyright © 1996, 2001, Sergei Kuchin, email: skuchin@sprynet.com, kuchin@hotmail.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.