⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 otl3_ex42.htm

📁 ISO_C++:C++_OTL开发文档
💻 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 ==&gt; CLOB. How to use
OTL namespaces)</title>
</head>
<body>
<center>
<h1>OTL 4.0, Example 42 (ODBC, OCI: LONG ==&gt; CLOB. How to use OTL
namespaces)</h1>
</center>
<h1>
<a name="example1"></a>Example 42 (ODBC, OCI: TEXT ==&gt; 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>
&nbsp;
</p>
<h2>Source Code</h2>
<pre>#include &lt;iostream&gt;<br>using namespace std;<br><br>#include &lt;stdio.h&gt;<br><br>#define OTL_ORA8 // Compile OTL 4.0/OCI8<br>#define OTL_ODBC // Compile OTL 4.0/ODBC<br>// In case when &lt;OTL_ORA8,OTL_ODBC&gt; or &lt;OTL_ORA7,OTL_ODBC&gt; 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.&nbsp;<br><br>#include &lt;otlv4.h&gt; // 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>{&nbsp;<br><br>&nbsp;<a
 href="otl3_long_string.htm">otl_long_string</a> f2(long_text_max); // define long string variable<br>&nbsp;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>&nbsp;odbc::<a
 href="otl3_stream_class.htm">otl_stream</a> o(1, // buffer size needs to be set to 1 for long strings<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "insert into test_tab values(:f1&lt;int&gt;,:f2&lt;varchar_long&gt;)",&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // SQL statement<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; db1 // connect object<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; );<br><br>&nbsp;for(int i=1;i&lt;=20;++i){<br>&nbsp; for(int j=0;j&lt;50000;++j)<br>&nbsp;&nbsp; f2[j]='*';<br>&nbsp; f2[50000]='?';<br>&nbsp; f2.<a
 href="otl3_long_string.htm#set_len">set_len</a>(50001);<br>&nbsp; o&lt;&lt;i&lt;&lt;f2;<br>&nbsp;}<br>}<br><br>void insert2()<br>// insert rows into OCI table<br>{&nbsp;<br><br>&nbsp;otl_long_string f2(long_text_max); // define long string variable<br>&nbsp;db2.set_max_long_size(long_text_max); // set maximum long string size for connect object<br>&nbsp;<br>&nbsp;int f1;<br><br>// INSERT statement for inserting rows into OCI table<br>&nbsp;oracle::otl_stream&nbsp;<br>&nbsp; o(1, // buffer size has to be set to 1 for operations with LOBs<br>&nbsp;&nbsp;&nbsp; "insert into test_tab2 values(:f1&lt;int&gt;,empty_clob()) "<br>&nbsp;&nbsp;&nbsp; "returning f2 into :f2&lt;clob&gt; ",<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // SQL statement<br>&nbsp;&nbsp;&nbsp;&nbsp; db2 // connect object<br>&nbsp;&nbsp;&nbsp; );<br><br>// SELECT statement to read rows from OCI table<br>&nbsp;odbc::otl_stream&nbsp;<br>&nbsp; i(1, // buffer size needs to be set to 1<br>&nbsp;&nbsp;&nbsp; "select * from test_tab", // SELECT statement<br>&nbsp;&nbsp;&nbsp; db1 // connect object<br>&nbsp;&nbsp; );&nbsp;<br><br>&nbsp;while(!i.eof()){<br>&nbsp; i&gt;&gt;f1&gt;&gt;f2; // Reading one row from ODBC<br>&nbsp; o&lt;&lt;f1&lt;&lt;f2; // Inserting one row into OCI<br>&nbsp;}<br><br>}<br><br>void select2()<br>// Select rows from OCI table<br>{&nbsp;<br>&nbsp;otl_long_string f2(long_text_max); // define long string variable<br>&nbsp;db2.set_max_long_size(long_text_max); // set maximum long string size for connect object<br><br>&nbsp;oracle::otl_stream&nbsp;<br>&nbsp; i(10, // buffer size. To read CLOBs, it can be set to a size greater than 1<br>&nbsp;&nbsp;&nbsp; "select * from test_tab2 where f1&gt;=:f&lt;int&gt; and f1&lt;=:f*2",<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // SELECT statement<br>&nbsp;&nbsp;&nbsp; db2 // connect object<br>&nbsp;&nbsp; );&nbsp;<br>&nbsp;&nbsp; // create select stream<br>&nbsp;<br>&nbsp;float f1;<br><br>&nbsp;i&lt;&lt;8; // assigning :f = 8<br>&nbsp;&nbsp; // SELECT automatically executes when all input variables are<br>&nbsp;&nbsp; // assigned. First portion of output rows is fetched to the buffer<br><br>&nbsp;while(!i.eof()){ // while not end-of-data<br>&nbsp; i&gt;&gt;f1&gt;&gt;f2;<br>&nbsp; cout&lt;&lt;"f1="&lt;&lt;f1&lt;&lt;", f2="&lt;&lt;f2[0]&lt;&lt;f2[f2.len()-1]&lt;&lt;", len="&lt;&lt;f2.len()&lt;&lt;endl;<br>&nbsp;}<br><br>&nbsp;i&lt;&lt;4; // assigning :f = 4<br>&nbsp;&nbsp; // SELECT automatically executes when all input variables are<br>&nbsp;&nbsp; // assigned. First portion of output rows is fetched to the buffer<br><br>&nbsp;while(!i.eof()){ // while not end-of-data<br>&nbsp; i&gt;&gt;f1&gt;&gt;f2;<br>&nbsp; cout&lt;&lt;"f1="&lt;&lt;f1&lt;&lt;", f2="&lt;&lt;f2[0]&lt;&lt;f2[f2.<a
 href="otl3_long_string.htm#len">len</a>()-1]&lt;&lt;", len="&lt;&lt;f2.len()&lt;&lt;endl;<br>&nbsp;}<br><br>}<br><br>int main()<br>{<br>&nbsp;oracle::otl_connect::<a
 href="otl3_connect_class.htm">otl_initialize</a>(); // initialize OCI environment<br>&nbsp;odbc::otl_connect::otl_initialize(); // initialize ODBC environment<br>&nbsp;try{<br><br>&nbsp; db1.<a
 href="otl3_connect_class.htm#rlogon">rlogon</a>("UID=scott;PWD=tiger;DSN=my_db"); // connect to ODBC<br>&nbsp; db2.rlogon("scott/tiger"); // connect to OCI<br><br>&nbsp; odbc::otl_cursor::direct_exec<br>&nbsp;&nbsp; (<br>&nbsp;&nbsp;&nbsp; db1,<br>&nbsp;&nbsp;&nbsp; "drop table test_tab",<br>&nbsp;&nbsp;&nbsp; odbc::otl_exception::disabled // disable OTL exceptions<br>&nbsp;&nbsp; ); // drop table<br><br>&nbsp; odbc::otl_cursor::<a
 href="otl3_const_sql.htm">direct_exec<br></a>&nbsp;&nbsp; (<br>&nbsp;&nbsp;&nbsp; db1,<br>&nbsp;&nbsp;&nbsp; "create table test_tab(f1 number, f2 long)"<br>&nbsp;&nbsp;&nbsp; );&nbsp; // create table<br><br>&nbsp; oracle::otl_cursor::direct_exec<br>&nbsp;&nbsp; (<br>&nbsp;&nbsp;&nbsp; db2,<br>&nbsp;&nbsp;&nbsp; "drop table test_tab2",<br>&nbsp;&nbsp;&nbsp; oracle::otl_exception::disabled // disable OTL exceptions<br>&nbsp;&nbsp; ); // drop table<br><br>&nbsp; oracle::otl_cursor::direct_exec<br>&nbsp;&nbsp; (<br>&nbsp;&nbsp;&nbsp; db2,<br>&nbsp;&nbsp;&nbsp; "create table test_tab2(f1 number, f2 clob)"<br>&nbsp;&nbsp;&nbsp; );&nbsp; // create table<br><br>&nbsp; insert1(); // insert records into MSSQL table<br>&nbsp; insert2(); // Read the ODBC table and insert records&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // into OCI table<br>&nbsp; select2(); // select records from the Oracle table.<br><br>&nbsp;}<br><br>&nbsp;catch(odbc::<a
 href="otl3_exception_class.htm">otl_exception</a>&amp; p){ // intercept OTL exceptions<br>&nbsp; cerr&lt;&lt;p.msg&lt;&lt;endl; // print out error message<br>&nbsp; cerr&lt;&lt;p.stm_text&lt;&lt;endl; // print out SQL that caused the error<br>&nbsp; cerr&lt;&lt;p.sqlstate&lt;&lt;endl; // print out SQlSTATE<br>&nbsp; cerr&lt;&lt;p.var_info&lt;&lt;endl; // print out the variable that caused the error<br>&nbsp;}<br><br>&nbsp;catch(oracle::otl_exception&amp; p){ // intercept OTL exceptions<br>&nbsp; cerr&lt;&lt;p.msg&lt;&lt;endl; // print out error message<br>&nbsp; cerr&lt;&lt;p.stm_text&lt;&lt;endl; // print out SQL that caused the error<br>&nbsp; cerr&lt;&lt;p.var_info&lt;&lt;endl; // print out the variable that caused the error<br>&nbsp;}<br><br>&nbsp;db1.logoff(); // disconnect from ODBC<br>&nbsp;db2.logoff(); // disconnect from OCI<br><br>&nbsp;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 &copy; 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 + -