OTL 4.0, Example 104 (#define OTL_EXENDED_EXCEPTION, ODBC / MS SQL Server)

Example 104 (#define OTL_EXTENDED_EXCPTION, ODBC / SQM SQL Server)

This example demonstrates #define OTL_EXTENDED_EXCEPTION. See also problem 47, and the otl_exception's extended fields.

Source Code

#include <iostream>
using namespace std;

#include <stdio.h>
#define OTL_ODBC_MSSQL_2008 // Compile OTL 4/ODBC, MS SQL 2008
//#define OTL_ODBC // Compile OTL 4/ODBC. Uncomment this when used with MS SQL 7.0/ 2000
#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@mssql2008"); // 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;

}

Output

[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











Examples Contents Go Home

Copyright © 1996-2024, Sergei Kuchin, email: skuchin@gmail.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. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.