📄 otl2.htm
字号:
}
</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 <iostream.h></font>
<font color="#FF0000">#include <stdio.h></font>
<font color="#FF0000">#include <otl.h></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<int,inout> := :A+1; "</font>
<font color="#008080">" :B<char[31],out> := :C<char[31],in>; "</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"><<</a>1<a href="#ref003"><<</a><font color="#008080">"Test String1"</font>; <i><font color="#804000">// assigning :A = 1, :C = "Test String1"</font></i>
o<a href="#ref003"><<</a>2<a href="#ref003"><<</a><font color="#008080">"Test String2"</font>; <i><font color="#804000">// assigning :A = 2, :C = "Test String2"</font></i>
o<a href="#ref003"><<</a>3<a href="#ref003"><<</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">>></a>a<a href="#ref004">>></a>b;
cout<<<font color="#008080">"A="</font><<a<<<font color="#008080">", B="</font><<b<<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<<<a href="#sec23">p.msg</a><<endl; <i><font color="#804000">// print out error message</font></i>
<font color="#0000A0">if</font>(p.stm_text)
cerr<<<a href="#sec23">p.stm_text</a><<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 <iostream.h></font>
<font color="#FF0000">#include <stdio.h></font>
<font color="#FF0000">#include <otl.h></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<int>,:f2<char[31]>)"</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<=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>=:f<int> and f1<=: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"><<</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<<<font color="#008080">"f1="</font><<f1<<<font color="#008080">", f2="</font><<f2<<endl;
}
i<a href="#ref003"><<</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<<<font color="#008080">"f1="</font><<f1<<<font color="#008080">", f2="</font><<f2<<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<<<a href="#sec23">p.msg</a><<endl; <i><font color="#804000">// print out error message</font></i>
<font color="#0000A0">if</font>(p.stm_text)
cerr<<<a href="#sec23">p.stm_text</a><<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 <iostream.h></font>
<font color="#FF0000">#include <stdio.h></font>
<font color="#FF0000">#include <otl.h></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<float>,:f2<char[31]>)"</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<=100;++i){
sprintf(tmp,<font color="#008080">"Name%d"</font>,i);
o<a href="#ref003"><<</a>(<font color="#0000A0">float</font>)i<a href="#ref003"><<</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>=:f<int> and f1<=: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"><<</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">>></a>f1<a href="#ref004">>></a>f2;
cout<<<font color="#008080">"f1="</font><<f1<<<font color="#008080">", f2="</font><<f2<<endl;
}
i<a href="#ref003"><<</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">>></a>f1<a href="#ref004">>></a>f2;
cout<<<font color="#008080">"f1="</font><<f1<<<font color="#008080">", f2="</font><<f2<<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<<<a href="#sec23">p.msg</a><<endl; <i><font color="#804000">// print out error message</font></i>
<font color="#0000A0">if</font>(p.stm_text)
cerr<<<a href="#sec23">p.stm_text</a><<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>
<li><a href="otl_it01.htm">otl_output_iterator</a></li>
<li><a href="otl_it02.htm">otl_input_iterator</a></li>
</ul>
<p>
These two iterator classes make it possible to combine the power of <a
href="http://www.metabyte.com/~fbp/stl/readings.html">generic
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -