📄 otl2odbc.htm
字号:
<p>
This example works with Oracle 8.x and SQL Server 6.5/7.0 without any
modifications to the table structure or datatypes.
</p>
<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(:1<int>,:2<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>=:1<int> and f1<=:2<int>*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<a href="#ref003"><<</a>8; <i><font color="#804000">// assigning :1 = 8, :2 = 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<a href="#ref003"><<</a>4; <i><font color="#804000">// assigning :1 = 4, :2 = 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 ODBC environment</font></i>
<font color="#0000A0">try</font>{
db.<a href="#ref007">rlogon</a>(<font color="#008080">"UID=SCOTT;PWD=TIGER;DSN=MY_DB"</font>); <i><font color="#804000">// connect to data source MY_DB</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 int, f2 varchar(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>
cerr<<<a href="#sec23">p.code</a><<endl; <i><font color="#804000">// print out error code</font></i>
cerr<<<a href="#sec23">p.sqlstate</a><<endl; <i><font color="#804000">// print out SQLSTATE 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 the data source</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>
<a name="sec254a"><h3>Example 4a (with otl_stream class,
TIMESTAMP_STRUCT and Oracle)</h3>
<p>
This example works with Oracle 8.x ODBC drivers only.
</p>
<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="ref101a"></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(3, <i><font color="#804000">// buffer size</font></i>
<font color="#008080">"insert into test_tab2 values(:1<float>,:2<timestamp>)"</font>,
<i><font color="#804000">// SQL statement</font></i>
db <i><font color="#804000">// connect object</font></i>
);
TIMESTAMP_STRUCT tm;
<font color="#0000A0">for</font>(<font color="#0000A0">int</font> i=1;i<=10;++i){
tm.year=1998;
tm.month=10;
tm.day=19;
tm.hour=23;
tm.minute=12;
tm.second=12;
tm.fraction=0;
o<a href="#ref003"><<</a>(<font color="#0000A0">float</font>)i<a href="#ref003"><<</a>tm;
}
}
<a name="ref102a"></a><h4><font color="#0000A0">void</font> select()</h4>
{
<a href="#ref001">otl_stream</a> i(3, <i><font color="#804000">// buffer size</font></i>
<font color="#008080">"select * from test_tab2 where f2=:1<timestamp>"</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;
TIMESTAMP_STRUCT tm,f2;
tm.year=1998;
tm.month=10;
tm.day=19;
tm.hour=23;
tm.minute=12;
tm.second=12;
tm.fraction=0;
i<a href="#ref003"><<</a>tm; <i><font color="#804000">// assigning :1 = tm</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.month<<<font color="#008080">"/"</font><<f2.day<<<font color="#008080">"/"</font>
<<f2.year<<<font color="#008080">" "</font><<f2.hour<<<font color="#008080">":"</font><<f2.minute<<<font color="#008080">":"</font>
<<f2.second<<<font color="#008080">"."</font><<f2.fraction
<<endl;
}
}
<h4><font color="#0000A0">int</font> main()</h4>
{
<a href="#ref006">otl_connect::otl_initialize()</a>; <i><font color="#804000">// initialize ODBC environment</font></i>
<font color="#0000A0">try</font>{
db.<a href="#ref007">rlogon</a>(<font color="#008080">"UID=scott;PWD=tiger;DSN=my_db"</font>); <i><font color="#804000">// connect to data source MY_DB</font></i>
<a href="#sec24">otl_cursor::direct_exec</a>
(
db,
<font color="#008080">"drop table test_tab2"</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_tab2(f1 int, f2 date)"</font>
); <i><font color="#804000">// create table</font></i>
<a href="#ref101a">insert()</a>; <i><font color="#804000">// insert records into table</font></i>
<a href="#ref102a">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>
cerr<<<a href="#sec23">p.code</a><<endl; <i><font color="#804000">// print out error code</font></i>
cerr<<<a href="#sec23">p.sqlstate</a><<endl; <i><font color="#804000">// print out SQLSTATE 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>
}
<a href="#ref009">db.logoff()</a>; <i><font color="#804000">// disconnect from the data source</font></i>
<font color="#0000A0">return</font> 0;
}
</pre>
<h4>Output</h4>
<pre>
f1=1, f2=10/19/1998 23:12:12.0
f1=2, f2=10/19/1998 23:12:12.0
f1=3, f2=10/19/1998 23:12:12.0
f1=4, f2=10/19/1998 23:12:12.0
f1=5, f2=10/19/1998 23:12:12.0
f1=6, f2=10/19/1998 23:12:12.0
f1=7, f2=10/19/1998 23:12:12.0
f1=8, f2=10/19/1998 23:12:12.0
f1=9, f2=10/19/1998 23:12:12.0
f1=10, f2=10/19/1998 23:12:12.0
</pre>
<a name="sec254b"><h3>Example 4b (with otl_stream class,
TIMESTAMP_STRUCT and MS SQL Server)</h3>
<p>
This example works with MS SQL Server 6.5/7.0 only.
</p>
<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="ref101b"></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(3, <i><font color="#804000">// buffer size</font></i>
<font color="#008080">"insert into test_tab2 values(:1<float>,:2<timestamp>)"</font>,
<i><font color="#804000">// SQL statement</font></i>
db <i><font color="#804000">// connect object</font></i>
);
TIMESTAMP_STRUCT tm;
<font color="#0000A0">for</font>(<font color="#0000A0">int</font> i=1;i<=10;++i){
tm.year=1998;
tm.month=10;
tm.day=19;
tm.hour=23;
tm.minute=12;
tm.second=12;
tm.fraction=0;
o<a href="#ref003"><<</a>(<font color="#0000A0">float</font>)i<a href="#ref003"><<</a>tm;
}
}
<a name="ref102b"></a><h4><font color="#0000A0">void</font> select()</h4>
{
<a href="#ref001">otl_stream</a> i(3, <i><font color="#804000">// buffer size</font></i>
<font color="#008080">"select * from test_tab2 where f2=:1<timestamp>"</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;
TIMESTAMP_STRUCT tm,f2;
tm.year=1998;
tm.month=10;
tm.day=19;
tm.hour=23;
tm.minute=12;
tm.second=12;
tm.fraction=0;
i<a href="#ref003"><<</a>tm; <i><font color="#804000">// assigning :1 = tm</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.month<<<font color="#008080">"/"</font><<f2.day<<<font color="#008080">"/"</font>
<<f2.year<<<font color="#008080">" "</font><<f2.hour<<<font color="#008080">":"</font><<f2.minute<<<font color="#008080">":"</font>
<<f2.second<<<font color="#008080">"."</font><<f2.fraction
<<endl;
}
}
<h4><font color="#0000A0">int</font> main()</h4>
{
<a href="#ref006">otl_connect::otl_initialize()</a>; <i><font color="#804000">// initialize ODBC environment</font></i>
<font color="#0000A0">try</font>{
db.<a href="#ref007">rlogon</a>(<font color="#008080">"UID=sa;PWD=;DSN=mssql"</font>); <i><font color="#804000">// connect to data source MSSQL</font></i>
<a href="#sec24">otl_cursor::direct_exec</a>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -