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

📄 otl1.htm

📁 ISO_C++:C++_OTL开发文档
💻 HTM
📖 第 1 页 / 共 5 页
字号:

  otl_cursor::direct_exec
   (
    db,
    "create table test_tab(f1 number, f2 varchar2(30))"
    );  // create table

  insert(); // insert records into table
  select(); // select records from table

 }

 catch(otl_exception& p){ // intercept OTL exceptions
  cerr<<p.msg<<endl; // print out error message
 }

 db.logoff(); // disconnect from Oracle

 return 0;

} /* main */

</xmp>
<h4>Output</h4>
<xmp>

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

</xmp>


<h3><a name="sec214">2.1.4. Example 4 (with Oracle LONG columns)</h3>

<h4>Source code</h4>
<xmp>

#include <iostream.h>
#include <stdio.h>
#include <otl.h>

otl_connect db; // connect object

const int BUF_SIZE=50; // host array size
const int LONG_STR_SIZE=2000; // LONG string size


void insert()
// insert rows into table
{ 
 otl_float f1(":f1"); // float host variable f1
 otl_long_varchar<LONG_STR_SIZE> f2(":f2"); 
   // long varchar host variable f2

 otl_cursor o(db); // create cursor

 o.eparse("insert into test_tab values(:f1,:f2)",&f1,&f2,0); 
   // parse sql statement and bind variables f1 and f2 with the
   // statement. the variable list is NULL terminated

 for(int i=1;i<=9;++i){
  f1.v=i; // assign host variable f1
  f2.set_len(5); // set the long_varchar string length
  f2[0]='N'; f2[1]='a'; 
  f2[2]='m'; f2[3]='e';
  f2[4]='0'+i;
  o.exec();
  db.commit(); // commit transaction
 }
} /* insert */

void select()
{ 
 otl_float f1; // float f1
 otl_long_varchar<LONG_STR_SIZE> f2;
 otl_int f(":f"); // host variable f

 otl_select_cursor i(db); 
   // create specialized select cursor
 
 i.eparse("select * from test_tab where f1>=:f and f1<=:f*2",&f1,&f2,&f,0);
   // parse select statement, bind input variable f and output columns
   // f1, f2 with the statement. f1 is treated as column 1 in the
   // select list, f2 -- as column 2. The variable list is NULL terminated

 f.v=4; // assign 4 to f

 while(i.next()){ // while not end-of-data
  cout<<"f1="<<f1.v<<", f2=";
  for(int j=0;j<f2.len();++j)
   cout<<f2[j]; // print out the long_varchar string
  cout<<endl;
 }

} /* select */

int main()
{

 try{

  db.rlogon("scott/tiger"); // connect to Oracle

  otl_cursor::direct_exec
   (
    db,
    "drop table test_tab",
    otl_exception::disabled // disable OTL exceptions
   ); // drop table

  otl_cursor::direct_exec
   (
    db,
    "create table test_tab(f1 number, f2 long)"
    );  // create table

  insert(); // insert records into table
  select(); // select records from table

 }

 catch(otl_exception& p){ // intercept OTL exceptions
  cerr<<p.msg<<endl; // print out error message
 }

 db.logoff(); // disconnect from Oracle

 return 0;

} /* main */

</xmp>
<h4>Output</h4>
<xmp>

f1=4, f2=Name4
f1=5, f2=Name5
f1=6, f2=Name6
f1=7, f2=Name7
f1=8, f2=Name8

</xmp>

<h3><a name="sec215">2.1.5. Example 5 (with the otl_select_stream class)</h3>

<h4>Source code</h4>
<xmp>

#include <iostream.h>
#include <stdio.h>
#include <otl.h>

otl_connect db; // connect object

const int BUF_SIZE=50; // host array size
const int STR_SIZE=31; // string size


void insert()
// insert rows into table
{ 
 otl_float_array<BUF_SIZE> f1(":f1"); // float host array f1

 otl_cstring_array<BUF_SIZE,STR_SIZE> f2(":f2"); // C-string host array f2
 otl_cursor o(db); // create cursor
 int n=0;

 o.eparse("insert into test_tab values(:f1,:f2)",&f1,&f2,0); 
   // parse sql statement and bind variables f1 and f2 with the
   // statement. the variable list is NULL terminated

 for(int i=1;i<=100;++i){
  ++n;
  f1.v[n-1]=i; // fill out host array f1
  sprintf(f2.v[n-1],"Name%d",i); // fill out host array f2
  if(n==BUF_SIZE){ // execute sql statement when buffer gets
                   // full
   o.exec(n);
   n=0;
  }
 }
 if(n>0) o.exec(n);
 db.commit(); // commit transaction
} /* insert */

void select()
{ 
 otl_int f(":f"); // host variable f
 otl_select_stream i(db,
		     "select * from test_tab where f1>=:f and f1<=:f*2",
		     BUF_SIZE, // size of the buffer attached to the
			       // stream 
		     &f, // input variable :f2
		     0 // NULL terminator of the variable list
		    ); 
   // create select stream
 
 int f1;
 char f2[STR_SIZE];

 f.v=8; // assign 8 to f

 i.rewind(); // rewind the stream: re-execute the statement and fetch
	     // first portion of rows.

 while(!i.eof()){ // while not end-of-data
  i>>f1>>f2;
  cout<<"f1="<<f1<<", f2="<<f2<<endl;
 }

 f.v=4; // assign 4 to f

 i.rewind(); // rewind the stream: re-execute the statement and fetch
	     // first portion of rows.

 while(!i.eof()){ // while not end-of-data
  i>>f1>>f2;
  cout<<"f1="<<f1<<", f2="<<f2<<endl;
 }

} /* select */

int main()
{

 try{

  db.rlogon("scott/tiger"); // connect to Oracle

  otl_cursor::direct_exec
   (
    db,
    "drop table test_tab",
    otl_exception::disabled // disable OTL exceptions
   ); // drop table

  otl_cursor::direct_exec
   (
    db,
    "create table test_tab(f1 number, f2 varchar2(30))"
    );  // create table

  insert(); // insert records into table
  select(); // select records from table

 }

 catch(otl_exception& p){ // intercept OTL exceptions
  cerr<<p.msg<<endl; // print out error message
 }

 db.logoff(); // disconnect from Oracle

 return 0;

} /* main */

</xmp>
<h4>Output</h4>
<xmp>

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

</xmp>

<h3><a name="sec216">2.1.6. Example 6 (with the otl_out_stream class)</h3>

<h4>Source code</h4>
<xmp>

#include <iostream.h>
#include <stdio.h>
#include <otl.h>

otl_connect db; // connect object

const int BUF_SIZE=50; // host array size
const int STR_SIZE=31; // string size


void insert()
// insert rows into table
{ 
 otl_float_array<BUF_SIZE> f1(":f1"); // float host array f1
 otl_cstring_array<BUF_SIZE,STR_SIZE> f2(":f2"); // C-string host array f2

 otl_out_stream o(db, // connect object
		  "insert into test_tab values(:f1,:f2)", // SQL statement
		  &f1, // bind variable :f1
		  &f2, // bind variable :f2
		  0 // end of variable list
		 );

 char tmp[31];

 for(int i=1;i<=100;++i){
  sprintf(tmp,"Name%d",i);
  o<<(float)i<<tmp;
 }
} /* insert */

void select()
{ 
 otl_int f(":f"); // host variable f
 otl_select_stream i(db,
		     "select * from test_tab where f1>=:f and f1<=:f*2",
		     BUF_SIZE, // size of the buffer attached to the
			       // stream 
		     &f, // input variable :f2
		     0 // NULL terminator of the variable list
		    ); 
   // create select stream
 
 int f1;
 char f2[STR_SIZE];

 f.v=8; // assign 8 to f

 i.rewind(); // rewind the stream: re-execute the statement and fetch
	     // first portion of rows.

 while(!i.eof()){ // while not end-of-data
  i>>f1>>f2;
  cout<<"f1="<<f1<<", f2="<<f2<<endl;
 }

 f.v=4; // assign 4 to f

 i.rewind(); // rewind the stream: re-execute the statement and fetch
	     // first portion of rows.

 while(!i.eof()){ // while not end-of-data
  i>>f1>>f2;
  cout<<"f1="<<f1<<", f2="<<f2<<endl;
 }

} /* select */

int main()
{

 try{

  db.rlogon("scott/tiger"); // connect to Oracle

  otl_cursor::direct_exec
   (
    db,
    "drop table test_tab",
    otl_exception::disabled // disable OTL exceptions
   ); // drop table

  otl_cursor::direct_exec
   (
    db,
    "create table test_tab(f1 number, f2 varchar2(30))"
    );  // create table

  insert(); // insert records into table
  select(); // select records from table

 }

 catch(otl_exception& p){ // intercept OTL exceptions
  cerr<<p.msg<<endl; // print out error message
 }

 db.logoff(); // disconnect from Oracle

 return 0;

} /* main */

</xmp>
<h4>Output</h4>
<xmp>

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

</xmp>

<h3><a name="sec217">2.1.7. Example 7 (with the otl_inout_stream class)</h3>

<h4>Source code</h4>
<xmp>

#include <iostream.h>
#include <stdio.h>
#include <otl.h>

otl_connect db; // connect object

void insert()
// insert rows into table
{ 
 otl_inout_stream o(50, // buffer size
		    "insert into test_tab values(:f1<float>,:f2<char[31]>)", 
		       // SQL statement
		    db // connect object
		   );
 char tmp[32];

 for(int i=1;i<=100;++i){
  sprintf(tmp,"Name%d",i);
  o<<(float)i<<tmp;
 }
} /* insert */

void select()
{ 
 otl_select_stream i(50, // buffer size
		     "select * from test_tab where f1>=:f<int> and f1<=:f*2",
		        // SELECT statement
		     db // connect object
		    ); 
   // create select stream
 
 int f1;
 char f2[31];

 i<<8; // assigning :f = 8
   // SELECT automatically executes when all input variables are
   // assigned. First portion of out rows is fetched to the buffer

 while(!i.eof()){ // while not end-of-data
  i>>f1>>f2;
  cout<<"f1="<<f1<<", f2="<<f2<<endl;
 }
 
 i<<4; // assigning :f = 4
   // SELECT automatically re-executes when all input variables are
   // assigned. First portion of out rows is fetched to the buffer

 while(!i.eof()){ // while not end-of-data
  i>>f1>>f2;
  cout<<"f1="<<f1<<", f2="<<f2<<endl;
 }

} /* select */

int main()
{

 try{

  db.rlogon("scott/tiger"); // connect to Oracle

  otl_cursor::direct_exec
   (
    db,
    "drop table test_tab",
    otl_exception::disabled // disable OTL exceptions
   ); // drop table

  otl_cursor::direct_exec
   (
    db,
    "create table test_tab(f1 number, f2 varchar2(30))"
    );  // create table

  insert(); // insert records into table
  select(); // select records from table

 }

 catch(otl_exception& p){ // intercept OTL exceptions
  cerr<<p.msg<<endl; // print out error message
 }

 db.logoff(); // disconnect from Oracle

 return 0;

} /* main */

</xmp>
<h4>Output</h4>
<xmp>

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

</xmp>


⌨️ 快捷键说明

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