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

📄 otl2.htm

📁 otl是c++数据库封装好的一个数据库接口
💻 HTM
📖 第 1 页 / 共 4 页
字号:
 <a href="#ref009">db.logoff()</a>; <i><font color="#804000">// disconnect from Oracle</font></i>

 <font color="#0000A0">return</font> 0;

}

</pre>
<h4>Output</h4>
<pre>

f1=8, f2=Name8
f1=9, f2=Name9
f1=10, f2=Name10
f1=11, f2=Name11
f1=12, f2=Name12
f1=13, f2=Name13
f1=14, f2=Name14
f1=15, f2=Name15
f1=16, f2=Name16
f1=4, f2=Name4
f1=5, f2=Name5
f1=6, f2=Name6
f1=7, f2=Name7
f1=8, f2=Name8

</pre>

<h3>Example 2 (with PL/SQL block)</h3>

<h4>Source code</h4>
<pre>

<font color="#FF0000">#include &lt;iostream.h&gt;</font>
<font color="#FF0000">#include &lt;stdio.h&gt;</font>
<font color="#FF0000">#include &lt;otl.h&gt;</font>

<a href="#ref002">otl_connect</a> db; <i><font color="#804000">// connect object</font></i>

<a name="ref201"></a><h4><font color="#0000A0">void</font> plsql()</h4>
<i><font color="#804000">// invoking PL/SQL block</font></i>
{ 
 <a href="#ref001">otl_stream</a> o(5, <i><font color="#804000">// buffer size</font></i>
	      <font color="#008080">"begin "</font>
	      <font color="#008080">" :A&lt;int,inout&gt; := :A+1; "</font>
	      <font color="#008080">" :B&lt;char[31],out&gt; := :C&lt;char[31],in&gt;; "</font>
	      <font color="#008080">"end;"</font>,
	         <i><font color="#804000">// PL/SQL block</font></i>
	      db <i><font color="#804000">// connect object</font></i>
	     );
 o<a href="#ref003">&lt;&lt;</a>1<a href="#ref003">&lt;&lt;</a><font color="#008080">"Test String1"</font>; <i><font color="#804000">// assigning :A = 1, :C = "Test String1"</font></i>
 o<a href="#ref003">&lt;&lt;</a>2<a href="#ref003">&lt;&lt;</a><font color="#008080">"Test String2"</font>; <i><font color="#804000">// assigning :A = 2, :C = "Test String2"</font></i>
 o<a href="#ref003">&lt;&lt;</a>3<a href="#ref003">&lt;&lt;</a><font color="#008080">"Test String3"</font>; <i><font color="#804000">// assigning :A = 3, :C = "Test String3"</font></i>

 o.<a href="#ref010">flush()</a>; <i><font color="#804000">// executing PL/SQL block 3 times</font></i>

 <font color="#0000A0">int</font> a;
 <font color="#0000A0">char</font> b[32];

 <font color="#0000A0">while</font>(!o.<a href="#ref005">eof()</a>){ <i><font color="#804000">// not end-of-data</font></i>
  o<a href="#ref004">&gt;&gt;</a>a<a href="#ref004">&gt;&gt;</a>b;
  cout&lt;&lt;<font color="#008080">"A="</font>&lt;&lt;a&lt;&lt;<font color="#008080">", B="</font>&lt;&lt;b&lt;&lt;endl;
 }

}

<h4><font color="#0000A0">int</font> main()</h4>
{
 <a href="#ref006">otl_connect::otl_initialize()</a>; <i><font color="#804000">// initialize OCI environment</font></i>
 <font color="#0000A0">try</font>{
  db.<a href="#ref007">rlogon</a>(<font color="#008080">"scott/tiger"</font>); <i><font color="#804000">// connect to Oracle</font></i>
  <a href="#ref201">plsql()</a>; <i><font color="#804000">// invoking PL/SQL block</font></i>
 }
 <font color="#0000A0">catch</font>(<a href="#sec23">otl_exception</a>& p){ <i><font color="#804000">// intercept OTL exceptions</font></i>
  cerr&lt;&lt;<a href="#sec23">p.msg</a>&lt;&lt;endl; <i><font color="#804000">// print out error message</font></i>
  <font color="#0000A0">if</font>(p.stm_text)
   cerr&lt;&lt;<a href="#sec23">p.stm_text</a>&lt;&lt;endl; <i><font color="#804000">// print out SQL that caused the error</font></i>
 }
 db.<a href="#ref009">logoff()</a>; <i><font color="#804000">// disconnect from Oracle</font></i>
 <font color="#0000A0">return</font> 0;
} 

</pre>
<h4>Output</h4>
<pre>

A=2, B=Test String1
A=3, B=Test String2
A=4, B=Test String3

</pre>

<h3>Example 3 (with printf/scanf functions)</h3>

<h4>Source code</h4>
<pre>

<font color="#FF0000">#include &lt;iostream.h&gt;</font>
<font color="#FF0000">#include &lt;stdio.h&gt;</font>
<font color="#FF0000">#include &lt;otl.h&gt;</font>

<a href="#ref002">otl_connect</a> db; <i><font color="#804000">// connect object</font></i>

<a name="ref301"></a><h4><font color="#0000A0">void</font> insert()</h4>
<i><font color="#804000">// insert rows into table</font></i>
{ 
 <a href="#ref001">otl_stream</a> o(50, <i><font color="#804000">// buffer size</font></i>
	      <font color="#008080">"insert into test_tab values(:f1&lt;int&gt;,:f2&lt;char[31]&gt;)"</font>, 
	         <i><font color="#804000">// SQL statement</font></i>
	      db <i><font color="#804000">// connect object</font></i>
	     );
 <font color="#0000A0">char</font> tmp[32];

 <font color="#0000A0">for</font>(<font color="#0000A0">int</font> i=1;i&lt;=100;++i){
  sprintf(tmp,<font color="#008080">"Name%d"</font>,i);
  o.<a href="#ref011">printf</a>(<font color="#008080">"%d %s"</font>,i,tmp); <i><font color="#804000">// write one row into stream</font></i>
 }
}

<a name="ref302"></a><h4><font color="#0000A0">void</font> select()</h4>
{ 
 <a href="#ref001">otl_stream</a> i(50, <i><font color="#804000">// buffer size</font></i>
	      <font color="#008080">"select * from test_tab where f1&gt;=:f&lt;int&gt; and f1&lt;=:f*2"</font>,
	         <i><font color="#804000">// SELECT statement</font></i>
	      db <i><font color="#804000">// connect object</font></i>
	     ); 
   <i><font color="#804000">// create select stream</font></i>
 
 <font color="#0000A0">int</font> f1;
 <font color="#0000A0">char</font> f2[31];

 i<a href="#ref003">&lt;&lt;</a>8; <i><font color="#804000">// assigning :f = 8</font></i>
   <i><font color="#804000">// SELECT automatically executes when all input variables are</font></i>
   <i><font color="#804000">// assigned. First portion of out rows is fetched to the buffer</font></i>

 <font color="#0000A0">while</font>(!i.<a href="#ref005">eof()</a>){ <i><font color="#804000">// while not end-of-data</font></i>
  i.<a href="#ref011">scanf</a>(<font color="#008080">"%d %s"</font>,&f1,f2); <i><font color="#804000">// read one row from stream</font></i>
  cout&lt;&lt;<font color="#008080">"f1="</font>&lt;&lt;f1&lt;&lt;<font color="#008080">", f2="</font>&lt;&lt;f2&lt&lt;endl;
 }
 
 i<a href="#ref003">&lt;&lt;</a>4; <i><font color="#804000">// assigning :f = 4</font></i>
   <i><font color="#804000">// SELECT automatically re-executes when all input variables are</font></i>
   <i><font color="#804000">// assigned. First portion of out rows is fetched to the buffer</font></i>

 <font color="#0000A0">while</font>(!i.<a href="#ref005">eof()</a>){ <i><font color="#804000">// while not end-of-data</font></i>
  i.<a href="#ref011">scanf</a>(<font color="#008080">"%d %s"</font>,&f1,f2); <i><font color="#804000">// read one row from stream</font></i>
  cout&lt;&lt;<font color="#008080">"f1="</font>&lt;&lt;f1&lt;&lt;<font color="#008080">", f2="</font>&lt;&lt;f2&lt;&lt;endl;
 }

}

<h4><font color="#0000A0">int</font> main()</h4>
{
 <a href="#ref006">otl_connect::otl_initialize()</a>; <i><font color="#804000">// initialize OCI environment</font></i>

 <font color="#0000A0">try</font>{

  db.<a href="#ref007">rlogon</a>(<font color="#008080">"scott/tiger"</font>); <i><font color="#804000">// connect to Oracle</font></i>

  <a href="#sec24">otl_cursor::direct_exec</a>
   (
    db,
    <font color="#008080">"drop table test_tab"</font>,
    <a href="#ref008">otl_exception::disabled</a> <i><font color="#804000">// disable OTL exceptions</font></i>
   ); <i><font color="#804000">// drop table</font></i>

  <a href="#sec24">otl_cursor::direct_exec</a>
   (
    db,
    <font color="#008080">"create table test_tab(f1 number, f2 varchar2(30))"</font>
    );  <i><font color="#804000">// create table</font></i>

  <a href="#ref301">insert()</a>; <i><font color="#804000">// insert records into table</font></i>
  <a href="#ref302">select()</a>; <i><font color="#804000">// select records from table</font></i>

 }

 <font color="#0000A0">catch</font>(<a href="#sec23">otl_exception</a>& p){ <i><font color="#804000">// intercept OTL exceptions</font></i>
  cerr&lt;&lt;<a href="#sec23">p.msg</a>&lt;&lt;endl; <i><font color="#804000">// print out error message</font></i>
  <font color="#0000A0">if</font>(p.stm_text)
   cerr&lt;&lt;<a href="#sec23">p.stm_text</a>&lt;&lt;endl; <i><font color="#804000">// print out SQL that caused the error</font></i>
 }

 db.<a href="#ref009">logoff()</a>; <i><font color="#804000">// disconnect from Oracle</font></i>

 <font color="#0000A0">return</font> 0;

}

</pre>
<h4>Output</h4>
<pre>

f1=8, f2=Name8
f1=9, f2=Name9
f1=10, f2=Name10
f1=11, f2=Name11
f1=12, f2=Name12
f1=13, f2=Name13
f1=14, f2=Name14
f1=15, f2=Name15
f1=16, f2=Name16
f1=4, f2=Name4
f1=5, f2=Name5
f1=6, f2=Name6
f1=7, f2=Name7
f1=8, f2=Name8

</pre>

<h3>Example 4 (with otl_stream class and referenced cursor)</h3>
<h4>Source code</h4>
<pre>

<font color="#FF0000">#include &lt;iostream.h&gt;</font>
<font color="#FF0000">#include &lt;stdio.h&gt;</font>
<font color="#FF0000">#include &lt;otl.h&gt;</font>

<a href="#ref002">otl_connect</a> db; <i><font color="#804000">// connect object</font></i>

<a name="ref401"></a><h4><font color="#0000A0">void</font> insert()</h4>
<i><font color="#804000">// insert rows into table</font></i>
{ 
 <a href="#ref001">otl_stream</a> o(50, <i><font color="#804000">// buffer size</font></i>
	      <font color="#008080">"insert into test_tab values(:f1&lt;float&gt;,:f2&lt;char[31]&gt;)"</font>, 
	         <i><font color="#804000">// SQL statement</font></i>
	      db <i><font color="#804000">// connect object</font></i>
	     );
 <font color="#0000A0">char</font> tmp[32];

 <font color="#0000A0">for</font>(int i=1;i&lt;=100;++i){
  sprintf(tmp,<font color="#008080">"Name%d"</font>,i);
  o<a href="#ref003">&lt;&lt;</a>(<font color="#0000A0">float</font>)i<a href="#ref003">&lt;&lt;</a>tmp;
 }
}

<a name="ref402"></a><h4><font color="#0000A0">void</font> select()</h4>
{ 
 <a href="#ref001">otl_stream</a> i(50, <i><font color="#804000">// buffer size</font></i>
	      <font color="#008080">"begin "</font>
              <font color="#008080">" open :cur for "</font>
              <font color="#008080">"  select * "</font>
              <font color="#008080">"  from test_tab "</font>
              <font color="#008080">"  where f1&gt;=:f&lt;int&gt; and f1&lt;=:f*2; "</font>
              <font color="#008080">"end;"</font>, 
	         <i><font color="#804000">// PL/SQL block returns a referenced cursor</font></i>
	      db, <i><font color="#804000">// connect object</font></i>
              <font color="#008080">":cur"</font> <i><font color="#804000">// referenced cursor placeholder name</font></i>
	     ); 
   <i><font color="#804000">// create select stream</font></i>
 
 <font color="#0000A0">int</font> f1;
 <font color="#0000A0">char</font> f2[31];

 i<a href="#ref003">&lt;&lt;</a>8; <i><font color="#804000">// assigning :f = 8</font></i>
   <i><font color="#804000">// SELECT automatically executes when all input variables are</font></i>
   <i><font color="#804000">// assigned. First portion of out rows is fetched to the buffer</font></i>

 <font color="#0000A0">while</font>(!i.<a href="#ref005">eof()</a>){ <i><font color="#804000">// while not end-of-data</font></i>
  i<a href="#ref004">&gt;&gt;</a>f1<a href="#ref004">&gt;&gt;</a>f2;
  cout&lt;&lt;<font color="#008080">"f1="</font>&lt;&lt;f1&lt;&lt;<font color="#008080">", f2="</font>&lt;&lt;f2&lt;&lt;endl;
 }
 
 i<a href="#ref003">&lt;&lt;</a>4; <i><font color="#804000">// assigning :f = 4</font></i>
   <i><font color="#804000">// SELECT automatically re-executes when all input variables are</font></i>
   <i><font color="#804000">// assigned. First portion of out rows is fetched to the buffer</font></i>

 <font color="#0000A0">while</font>(!i.<a href="#ref005">eof()</a>){ <i><font color="#804000">// while not end-of-data</font></i>
  i<a href="#ref004">&gt;&gt;</a>f1<a href="#ref004">&gt;&gt;</a>f2;
  cout&lt;&lt;<font color="#008080">"f1="</font>&lt;&lt;f1&lt;&lt;<font color="#008080">", f2="</font>&lt;&lt;f2&lt;&lt;endl;
 }

}

<h4><font color="#0000A0">int</font> main()</h4>
{
 <a href="#ref006">otl_connect::otl_initialize()</a>; <i><font color="#804000">// initialize OCI environment</font></i>

 <font color="#0000A0">try</font>{

  db.<a href="#ref007">rlogon</a>(<font color="#008080">"scott/tiger"</font>); <i><font color="#804000">// connect to Oracle</font></i>

  <a href="#sec24">otl_cursor::direct_exec</a>
   (
    db,
    <font color="#008080">"drop table test_tab"</font>,
    <a href="#ref008">otl_exception::disabled</a> <i><font color="#804000">// disable OTL exceptions</font></i>
   ); <i><font color="#804000">// drop table</font></i>

  <a href="#sec24">otl_cursor::direct_exec</a>
   (
    db,
    <font color="#008080">"create table test_tab(f1 number, f2 varchar2(30))"</font>
    );  <i><font color="#804000">// create table</font></i>

  <a href="#ref401">insert()</a>; <i><font color="#804000">// insert records into table</font></i>
  <a href="#ref402">select()</a>; <i><font color="#804000">// select records from table</font></i>

 }

 <font color="#0000A0">catch</font>(<a href="#sec23">otl_exception</a>& p){ <i><font color="#804000">// intercept OTL exceptions</font></i>
  cerr&lt;&lt;<a href="#sec23">p.msg</a>&lt;&lt;endl; <i><font color="#804000">// print out error message</font></i>
  <font color="#0000A0">if</font>(p.stm_text)
   cerr&lt;&lt;<a href="#sec23">p.stm_text</a>&lt;&lt;endl; <i><font color="#804000">// print out SQL that caused the error</font></i>
 }

 db.<a href="#ref009">logoff()</a>; <i><font color="#804000">// disconnect from Oracle</font></i>

 <font color="#0000A0">return</font> 0;

}

</pre>
<h4>Output</h4>
<pre>

f1=8, f2=Name8
f1=9, f2=Name9
f1=10, f2=Name10
f1=11, f2=Name11
f1=12, f2=Name12
f1=13, f2=Name13
f1=14, f2=Name14
f1=15, f2=Name15
f1=16, f2=Name16
f1=4, f2=Name4
f1=5, f2=Name5
f1=6, f2=Name6
f1=7, f2=Name7
f1=8, f2=Name8

</pre>

<h1><a name="sec3">3. STL-compliant iterators for otl_stream </h1>
<p>
OTL provides two <a href="#sec22">otl_stream</a> based <a
href="http://www.sgi.com/Technology/STL/Iterators.html">STL-compliant
iterator</a> classes:
</p>
<ul>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -