📄 otl4_ex678.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title>OTL 4.0, Example 678 (XMLType values > 4000 bytes as
CLOB/std:string on SELECT, and on INSERT)</title>
<meta name="Author" content="Sergei Kuchin">
<meta name="GENERATOR"
content="Mozilla/3.03Gold (Win95; I) [Netscape]">
<meta name="KeyWords"
content="OTL, Oracle, ODBC, DB2, CLI, database API, C++, Template Library">
</head>
<body>
<h1 align="center">OTL 4.0, Example 678 (XMLType values > 4000 bytes
as CLOB/std::string on
SELECT and on INSERT)</h1>
<p>This example demonstrates simple INSERT and SELECT statements with
the large XMLType data type values (>400 bytes) as well as constant
SQL statements. <br>
</p>
<h2>Source Code</h2>
<pre>#include <iostream><br>#include <string><br>using namespace std;<br><br>#include <stdio.h><br>#define OTL_STL<br>#define OTL_ORA9I // Compile OTL 4.0/OCI9I<br>//#define OTL_ORA10G // Compile OTL 4.0/OCI10g<br>//#define OTL_ORA10G_R2 // Compile OTL 4.0/OCI10gR2<br>//#define OTL_ORA11G // Compile OTL 4.0/OCI11G<br>#include <otlv4.h> // include the OTL 4.0 header file<br><br><a
href="otl3_connect_class.htm">otl_connect</a> db; // connect object<br><br>void insert()<br>// insert rows into table<br>{ <br> db.<a
href="otl3_connect_class.htm#set_max_long_size">set_max_long_size</a>(20000); // set maximum long string size for connect object<br> <a
href="otl3_stream_class.htm">otl_stream</a> o1(1, // buffer size needs to be set to 1<br> "insert into temp_clob values(empty_clob()) "<br> "returning temp_clob into :temp_clob<clob>",<br> // SQL statement<br> db // connect object<br> );<br> o1.set_commit(0); // set stream's auto-commit to OFF<br> otl_stream o2(1, // buffer size needs to be set to 1<br> "insert into test_tab "<br> "select :f1<int>, XMLType(temp_clob) from temp_clob",<br> // SQL statement<br> db // connect object<br> );<br> o2.set_commit(0); // set stream's auto-commit to OFF<br> string f2_str;<br> f2_str="<TAG>";<br> string tmp_str;<br> tmp_str.append(6000,'.');<br> f2_str+=tmp_str;<br> f2_str+="</TAG>";<br> // insert the large XML into a temporary CLOB in a temporary table.<br> o1<<f2_str;<br> for(int i=1;i<=2;++i)<br> o2<<i;<br> db.commit(); // commit transaction<br><br>}<br><br>void select()<br>{ <br> string f2;<br> db.<a
href="otl3_connect_class.htm#set_max_long_size">set_max_long_size</a>(20000); // set maximum long string size for connect object<br><br> <a
href="otl3_stream_class.htm">otl_stream</a> i(10, // buffer size. To read XML as CLOBs, it can be set to a size greater than 1<br> "select f1, xmltype.getclobval(f2) f2 "<br> "from test_tab ",<br> // SELECT statement<br> db // connect object<br> ); <br> // create select stream<br> <br> int f1;<br><br> while(!i.eof()){ // while not end-of-data<br> i>>f1>>f2;<br> cout<<"f1="<<f1<<", f2="<<f2<<endl;<br> }<br><br>}<br><br>int main()<br>{<br> <a
href="otl3_connect_class.htm">otl_connect::otl_initialize</a>(); // initialize OCI environment<br> try{<br><br> db.rlogon("scott/tiger"); // connect to Oracle<br><br> <a
href="otl3_const_sql.htm">otl_cursor::direct_exec<br></a> (<br> db,<br> "drop table test_tab",<br> otl_exception::disabled // disable OTL exceptions<br> ); // drop table<br><br> <a
href="otl3_const_sql.htm">otl_cursor::direct_exec<br></a> (<br> db,<br> "create table test_tab(f1 number, f2 xmltype)"<br> ); // create table<br><br>// create global temporary table for temporary CLOB values<br> db.direct_exec("drop table temp_clob",<br> otl_exception::disabled);<br> db.direct_exec("create global temporary table temp_clob(temp_clob clob) "<br> "ON COMMIT delete rows");<br><br><br> insert(); // insert records into table<br> select(); // select records from table<br><br> }<br><br> catch(<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.var_info<<endl; // print out the variable that caused the error<br> }<br><br> db.logoff(); // disconnect from Oracle<br><br> return 0;<br><br>}<br></pre>
<h2>Output</h2>
<pre>f1=1, f2=<TAG>................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................</TAG><br>f1=2, f2=<TAG>................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................</TAG><br><br><hr
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -