📄 otl3_ex42.htm
字号:
<!DOCTYPE html PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<meta name="Author" content="Sergei Kuchin">
<meta name="GENERATOR"
content="Mozilla/4.78 [en] (Win98; U) [Netscape]">
<meta name="KeyWords"
content="OTL, Oracle, ODBC, DB2, CLI, database API, C++, Template Library">
<title>OTL 4.0, Example 42 (ODBC, OCI: LONG ==> CLOB. How to use
OTL namespaces)</title>
</head>
<body>
<center>
<h1>OTL 4.0, Example 42 (ODBC, OCI: LONG ==> CLOB. How to use OTL
namespaces)</h1>
</center>
<h1>
<a name="example1"></a>Example 42 (ODBC, OCI: TEXT ==> CLOB. How to
use
OTL namespaces)</h1>
This example demonstrates how to use more than one instance of the OTL
library. The example opens connections to Oracle via OTL/ODBC as well
as
via OTL/OCI8. Then, a table with a LONG column gets created via ODBC,
filled
out with several rows, and the rows get copied over to a similar table
via OCI8.
<p>This example also demonstrates how to use the <a
href="otl3_long_string.htm">otl_long_string</a>
class to carry over values from ODBC to OCI8.
<br>
</p>
<h2>Source Code</h2>
<pre>#include <iostream><br>using namespace std;<br><br>#include <stdio.h><br><br>#define OTL_ORA8 // Compile OTL 4.0/OCI8<br>#define OTL_ODBC // Compile OTL 4.0/ODBC<br>// In case when <OTL_ORA8,OTL_ODBC> or <OTL_ORA7,OTL_ODBC> pair is<br>// defined, the OTL header file generates namespaces oracle:: and odbc::<br>// respectively, in order to make the names of the otl_xxx classes<br>// unique. <br><br>#include <otlv4.h> // include the OTL 4.0 header file<br><br>const int long_text_max=70000; // maximum length of long strings.<br><br>odbc::<a
href="otl3_connect_class.htm">otl_connect</a> db1; // connect object<br>oracle::otl_connect db2; // connect object<br><br>void insert1(void)<br>// insert rows into ODBC table<br>{ <br><br> <a
href="otl3_long_string.htm">otl_long_string</a> f2(long_text_max); // define long string variable<br> db1.<a
href="otl3_connect_class.htm#set_max_long_size">set_max_long_size</a>(long_text_max); // set maximum long string size for connect object<br><br> odbc::<a
href="otl3_stream_class.htm">otl_stream</a> o(1, // buffer size needs to be set to 1 for long strings<br> "insert into test_tab values(:f1<int>,:f2<varchar_long>)", <br> // SQL statement<br> db1 // connect object<br> );<br><br> for(int i=1;i<=20;++i){<br> for(int j=0;j<50000;++j)<br> f2[j]='*';<br> f2[50000]='?';<br> f2.<a
href="otl3_long_string.htm#set_len">set_len</a>(50001);<br> o<<i<<f2;<br> }<br>}<br><br>void insert2()<br>// insert rows into OCI table<br>{ <br><br> otl_long_string f2(long_text_max); // define long string variable<br> db2.set_max_long_size(long_text_max); // set maximum long string size for connect object<br> <br> int f1;<br><br>// INSERT statement for inserting rows into OCI table<br> oracle::otl_stream <br> o(1, // buffer size has to be set to 1 for operations with LOBs<br> "insert into test_tab2 values(:f1<int>,empty_clob()) "<br> "returning f2 into :f2<clob> ",<br> // SQL statement<br> db2 // connect object<br> );<br><br>// SELECT statement to read rows from OCI table<br> odbc::otl_stream <br> i(1, // buffer size needs to be set to 1<br> "select * from test_tab", // SELECT statement<br> db1 // connect object<br> ); <br><br> while(!i.eof()){<br> i>>f1>>f2; // Reading one row from ODBC<br> o<<f1<<f2; // Inserting one row into OCI<br> }<br><br>}<br><br>void select2()<br>// Select rows from OCI table<br>{ <br> otl_long_string f2(long_text_max); // define long string variable<br> db2.set_max_long_size(long_text_max); // set maximum long string size for connect object<br><br> oracle::otl_stream <br> i(10, // buffer size. To read CLOBs, it can be set to a size greater than 1<br> "select * from test_tab2 where f1>=:f<int> and f1<=:f*2",<br> // SELECT statement<br> db2 // connect object<br> ); <br> // create select stream<br> <br> float f1;<br><br> i<<8; // assigning :f = 8<br> // SELECT automatically executes when all input variables are<br> // assigned. First portion of output rows is fetched to the buffer<br><br> while(!i.eof()){ // while not end-of-data<br> i>>f1>>f2;<br> cout<<"f1="<<f1<<", f2="<<f2[0]<<f2[f2.len()-1]<<", len="<<f2.len()<<endl;<br> }<br><br> i<<4; // assigning :f = 4<br> // SELECT automatically executes when all input variables are<br> // assigned. First portion of output rows is fetched to the buffer<br><br> while(!i.eof()){ // while not end-of-data<br> i>>f1>>f2;<br> cout<<"f1="<<f1<<", f2="<<f2[0]<<f2[f2.<a
href="otl3_long_string.htm#len">len</a>()-1]<<", len="<<f2.len()<<endl;<br> }<br><br>}<br><br>int main()<br>{<br> oracle::otl_connect::<a
href="otl3_connect_class.htm">otl_initialize</a>(); // initialize OCI environment<br> odbc::otl_connect::otl_initialize(); // initialize ODBC environment<br> try{<br><br> db1.<a
href="otl3_connect_class.htm#rlogon">rlogon</a>("UID=scott;PWD=tiger;DSN=my_db"); // connect to ODBC<br> db2.rlogon("scott/tiger"); // connect to OCI<br><br> odbc::otl_cursor::direct_exec<br> (<br> db1,<br> "drop table test_tab",<br> odbc::otl_exception::disabled // disable OTL exceptions<br> ); // drop table<br><br> odbc::otl_cursor::<a
href="otl3_const_sql.htm">direct_exec<br></a> (<br> db1,<br> "create table test_tab(f1 number, f2 long)"<br> ); // create table<br><br> oracle::otl_cursor::direct_exec<br> (<br> db2,<br> "drop table test_tab2",<br> oracle::otl_exception::disabled // disable OTL exceptions<br> ); // drop table<br><br> oracle::otl_cursor::direct_exec<br> (<br> db2,<br> "create table test_tab2(f1 number, f2 clob)"<br> ); // create table<br><br> insert1(); // insert records into MSSQL table<br> insert2(); // Read the ODBC table and insert records <br> // into OCI table<br> select2(); // select records from the Oracle table.<br><br> }<br><br> catch(odbc::<a
href="otl3_exception_class.htm">otl_exception</a>& p){ // intercept OTL exceptions<br> cerr<<p.msg<<endl; // print out error message<br> cerr<<p.stm_text<<endl; // print out SQL that caused the error<br> cerr<<p.sqlstate<<endl; // print out SQlSTATE<br> cerr<<p.var_info<<endl; // print out the variable that caused the error<br> }<br><br> catch(oracle::otl_exception& p){ // intercept OTL exceptions<br> cerr<<p.msg<<endl; // print out error message<br> cerr<<p.stm_text<<endl; // print out SQL that caused the error<br> cerr<<p.var_info<<endl; // print out the variable that caused the error<br> }<br><br> db1.logoff(); // disconnect from ODBC<br> db2.logoff(); // disconnect from OCI<br><br> return 0;<br><br>}</pre>
<h2>
Output</h2>
<pre>f1=8, f2=*?, len=50001<br>f1=9, f2=*?, len=50001<br>f1=10, f2=*?, len=50001<br>f1=11, f2=*?, len=50001<br>f1=12, f2=*?, len=50001<br>f1=13, f2=*?, len=50001<br>f1=14, f2=*?, len=50001<br>f1=15, f2=*?, len=50001<br>f1=16, f2=*?, len=50001<br>f1=4, f2=*?, len=50001<br>f1=5, f2=*?, len=50001<br>f1=6, f2=*?, len=50001<br>f1=7, f2=*?, len=50001<br>f1=8, f2=*?, len=50001<br><br><br><br><br><br><br><br><br><hr
width="100%"></pre>
<center><a href="otl3_examples.htm">Examples</a> <a href="otl3.htm">Contents</a><a
href="home.htm">Go
Home</a></center>
<p>Copyright © 1996, 2008, Sergei Kuchin, email: <a
href="mailto:skuchin@aceweb.com">skuchin@aceweb.com</a>,
<a href="mailto:skuchin@gmail.com">skuchin@gmail.com
<script language="JavaScript"><!-- hide from old browsers
var modDate = new Date(document.lastModified)
document.write("<i> Last Updated:</i> " + (modDate.getMonth()+1) + "/" +
modDate.getDate() + "/" + "0"+(modDate.getYear())%100+".");
//-->
</script></a>.
</p>
<p><i>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.</i>
</p>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-5456201-1");
pageTracker._trackPageview();
</script>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -