📄 otl4_ex378.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title>OTL 4.0, Example 378 (Insert/Update/Select with Oracle
8i/9i/10g CLOB in stream mode and stored procs)</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 378 (Insert/Update/Select with
Oracle
8i/9i/10g CLOB in stream mode and stored procs)</h1>
<p>This example demonstrates PL/SQL (stored procs) INSERT, UPDATE, and
SELECT statements with
the Oracle 8i/9i/10g CLOB datatype in <a href="otl3_lob_stream.htm">stream
mode</a>.</p>
<h2>Source Code</h2>
<pre>#include <iostream><br>using namespace std;<br><br>#include <stdio.h></pre>
<pre>#define OTL_ORA8I // Compile OTL 4.0/OCI8i<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/OCI10g Release 2<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>{<a
href="otl3_long_string.htm">otl_long_string</a> f2(5000); // define long string variable<br> <a
href="otl3_stream_class.htm">otl_stream</a> o(1, // buffer size has to be set to 1 for operations with LOBs<br> "begin "<br> " insert_proc(:f1<int,in>,:f2<<a
href="otl3_bind_variables.htm#clob">clob</a>,in>); "<br> "end;", // PL/SQL block that calls a stored procedure.<br> db // connect object<br> );<br> o.<a
href="otl3_stream_class.htm#set_commit">set_commit</a>(0); // setting stream "auto-commit" to "off". It is required<br> // when LOB stream mode is used.<br><br> <a
href="otl3_lob_stream.htm">otl_lob_stream</a> lob; // LOB stream for reading/writing unlimited number<br> // of bytes regardless of the buffer size.<br><br> for(int i=1; i<=3; ++i){<br> for(int j=0;j<4000;++j)<br> f2[j]='*';<br> f2[4000]='?';<br> f2.<a
href="otl3_long_string.htm#set_len">set_len</a>(4001);<br> o<<<span
style="font-style: italic;">i</span>;<br> o<a
href="otl3_stream_class.htm#stream_write_lob"><<</a>lob; // Initialize otl_lob_stream by writing it into the otl_stream<br> lob.<a
href="file:///D%7C/oscl/vc/doc/otl4/otl3_lob_stream.htm#set_len">set_len</a>(4001); // setting the total size of of the CLOB to be written<br> lob<<f2; // writing f2 into lob<br> lob.<a
href="otl3_lob_stream.htm">close</a>(); // closing the otl_lob_stream<br> }<br> db.commit(); // committing transaction.<br>}</pre>
<pre>void update()<br>// insert rows in table<br>{ </pre>
<pre> <a href="otl3_long_string.htm">otl_long_string</a> f2(5200); // define long string variable<br><br> <a
href="otl3_stream_class.htm">otl_stream</a> o(1, // buffer size has to be set to 1 for operations with LOBs<br> "begin "<br> " update_proc(:f1<int,in>,:f2<<a
href="otl3_bind_variables.htm#clob">clob</a>,in>); "<br> "end;", // PL/SQL block that calls a stored procedure<br> db // connect object<br> );<br><br> <a
href="otl3_lob_stream.htm">otl_lob_stream</a> lob;<br><br> o.<a
href="otl3_stream_class.htm#set_commit">set_commit</a>(0); // setting stream "auto-commit" to "off". <br><br><br> for(int j=0;j<5000;++j){<br> f2[j]='#';<br> }<br> f2[5000]='?';<br> f2.<a
href="otl3_long_string.htm#set_len">set_len</a>(5001);<br> o<<2;<br> o<a
href="otl3_stream_class.htm#stream_write_lob"><<</a>lob; // Initialize otl_lob_stream by writing it<br> // into otl_stream.<br> lob.<a
href="otl3_lob_stream.htm#set_len">set_len</a>(5001*4); // setting the total size of of the CLOB to be written<br> for(int i=1;i<=4;++i)<br> lob<a
href="otl3_lob_stream.htm#write"><<</a>f2; // writing chunks of the CLOB into the otl_lob_stream<br> lob.<a
href="otl3_lob_stream.htm#close">close</a>(); // closing the otl_lob_stream<br><br> db.commit(); // committing transaction<br><br>}<br><br>void select(int f1)<br>{ <br> <a
href="otl3_long_string.htm">otl_long_string</a> f2(6000); // define long string variable<br><br> <a
href="otl3_stream_class.htm">otl_stream</a> i(1, // buffer size needs to be set to 1<br> "begin "<br> " select_proc(:f1<int,in>,:f2<<a
href="otl3_bind_variables.htm#clob">clob</a>,out>); "<br> "end;", // PL/SQL block that calls a stored procedure<br> db // connect object<br> ); <br> <br> <a
href="otl3_lob_stream.htm">otl_lob_stream</a> lob; // Stream for reading CLOB<br><br> i<<f1;<br> cout<<"f1="<<f1<<endl;<br> i<a
href="otl3_stream_class.htm#stream_read_lob">>></a>lob; // initializing CLOB stream by reading the CLOB reference <br> // into the otl_lob_stream from the otl_stream.<br> int n=0;<br> while(!lob.<a
href="otl3_lob_stream.htm#eof">eof</a>()){ // read while not "end-of-file" -- end of CLOB<br> ++n;<br> lob<a
href="otl3_lob_stream.htm#read">>></a>f2; // reading a chunk of CLOB<br> cout<<" chunk #"<<n;<br> cout<<", f2="<<f2[0]<<f2[f2.len()-1]<<", len="<<f2.len()<<endl;<br> }<br> lob.<a
href="otl3_lob_stream.htm#close">close</a>(); // closing the otl_lob_stream. This step may be skipped.<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<a
href="otl3_connect_class.htm#operator_ll"><<</a>"scott/tiger"; // connect to the database<br><br> // Send SQL statements to the connect obejct for immediate execution. <br> // Ignore any exception for the first statement.<br> try{ db<a
href="otl3_connect_class.htm#operator_ll"><<</a>"drop table test_tab"; } catch(<a
href="otl3_exception_class.htm">otl_exception</a>&){}<br> db<a
href="otl3_connect_class.htm#operator_ll"><<</a>"create table test_tab(f1 int, f2 clob)";<br> db<a
href="otl3_connect_class.htm#operator_ll"><<</a>"CREATE OR REPLACE PROCEDURE insert_proc "<br> " (A in NUMBER, "<br> " B in out nocopy CLOB) "<br> "IS "<br> "BEGIN "<br> " insert into TEST_TAB (F1, F2) "<br> " values (a, EMPTY_CLOB()) "<br> " returning F2 into B; "<br> "END;";<br> db<a
href="file:///D%7C/oscl/vc/doc/otl4/otl3_connect_class.htm#operator_ll"><<</a>"CREATE OR REPLACE PROCEDURE update_proc "<br> " (A in NUMBER, "<br> " B in out nocopy CLOB) "<br> "IS "<br> "BEGIN "<br> " select F2 "<br> " into B "<br> " from TEST_TAB "<br> " where F1 = A "<br> " for update nowait; "<br> "END;";<br> db<a
href="otl3_connect_class.htm#operator_ll"><<</a>"CREATE OR REPLACE PROCEDURE select_proc "<br> " (A in NUMBER, "<br> " B in out nocopy CLOB) "<br> "IS "<br> "BEGIN "<br> " select F2 "<br> " into B "<br> " from TEST_TAB "<br> " where f1 = A; "<br> "END;";<br> insert(); // insert records into table<br> update(); // update records in table<br> select(1); // select records from table<br> select(2); // select records from table<br> select(3); // 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<br> chunk #1, f2=*?, len=4001<br>f1=2<br> chunk #1, f2=##, len=6000<br> chunk #2, f2=##, len=6000<br> chunk #3, f2=##, len=6000<br> chunk #4, f2=#?, len=2004<br>f1=3<br> chunk #1, f2=*?, len=4001<br><hr
width="100%"></pre>
<center>
<p><a href="otl3_examples.htm">Examples</a> <a href="otl3.htm">Contents</a>
<a href="home.htm">Go Home</a> </p>
</center>
<p>Copyright © 1996, 2008, Sergei Kuchin, email: <a
href="mailto:skuchin@aceweb.com">skuchin@aceweb.com</a>,
<a href="mailto:skuchin@gmail.com">skuchin@gmail.com</a>
<script language="JavaScript"><!-- hide from oldgmailsers
var modDate = new Date(document.lastModified)
document.write("<i> Last Updated:</i> " + (modDate.getMonth()+1) + "/" +
modDate.getDate() + "/" + "0"+(modDate.getYear())%100+".");
//-->
</script>.</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>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -