使用OTL对oracle数据库进行增加,删除,编辑和查询操作。


#include "stdafx.h"
#include <iostream>
using namespace std;

#include <stdio.h>
//#define OTL_ORA10G
#define OTL_ORA9I // Compile OTL 4.0/OCI9i
#define OTL_ORA_UTF8 // Enable UTF8 OTL for OCI9i
#include <otlv4.h> // include the OTL 4.0 header file
#pragma comment(lib, "oci.lib")
otl_connect db; // connect object
#define CONNECTSTRING "test/[email protected]"

char strSelectSql[100]  = { "select f1,f2,f3 from test_tab"};
char strDeleteSql[100]  = { "delete from test_tab where f1 = '4' "};
char strUpdate[150]     = { "update test_tab set f2 = 'Olympiad in china' , f3 = to_date('2008-08-08 08:08:08','yyyy-mm-dd hh24:mi:ss') where f1 = 5 "};
void insert()
{
 otl_stream o;
 o.open(1,"INSERT INTO TEST_TAB(f1,f2,f3)values(4,'number one',to_date('2007-09-06 13:49:50','yyyy-mm-dd hh24:mi:ss'))",db);
    o.close();
 o.open(1,"INSERT INTO TEST_TAB(f1,f2,f3)values(5,'number one',to_date('2007-09-06 13:49:50','yyyy-mm-dd hh24:mi:ss'))",db);
}
void deleteR()
{
 otl_stream o(1,strDeleteSql,db);
}
void update()
{
 otl_stream o(1,strUpdate,db);
}
void select()
{
 otl_stream o(1,strSelectSql,db);
 int count = 0;
 int myf1;
 char myf2[30] = {""};
 char myf3[30] = {""};
 otl_datetime datetime;
 while(!o.eof())
 {
  o>>myf1;
  o>>myf2;
  o>>datetime;
  count++;
  sprintf(myf3,"%d-%02d-%02d %02d:%02d:%02d",datetime.year,datetime.month,datetime.day,datetime.hour,datetime.minute,datetime.second);
  cout<<"f1: "<<myf1<<", f2: "<<myf2<<", 时间: "<<myf3<<", 当前行数: "<<count<<endl;
 }
 return ;
}

int main()
{
    otl_connect::otl_initialize(); // initialize OCI environment
    try
 {
  db.rlogon(CONNECTSTRING); // connect to Oracle

  otl_cursor::direct_exec(db,"drop table test_tab",otl_exception::disabled); // drop table
  otl_cursor::direct_exec(db,"create table test_tab(f1 number, f2 nvarchar2(30), f3 date )");  // create table
     //before delete ......
  cout<<"before delete ... "<<endl;
  insert(); // insert records into table
     select(); // select records from table

  cout<<"after delete ... delete condition: f1 = 4 "<<endl;
  deleteR();
  select();
  
  cout<<endl;
  cout<<"before upate ... "<<endl;
  select();
  cout<<"after upate ... update condition: f1 = 5 "<<endl;
  update();
  select();
  
 }
 catch(otl_exception& p)
 { // intercept OTL exceptions
  cerr<<p.msg<<endl; // print out error message
  cerr<<p.stm_text<<endl; // print out SQL that caused the error
  cerr<<p.var_info<<endl; // print out the variable that caused the error
 }

 db.logoff(); // disconnect from Oracle
 return 0;
}
 

 

输出的结果为:

before delete ...
f1: 4, f2: number one, 时间: 2007-09-06 13:49:50, 当前行数: 1
f1: 5, f2: number one, 时间: 2007-09-06 13:49:50, 当前行数: 2
after delete ... delete condition: f1 = 4
f1: 5, f2: number one, 时间: 2007-09-06 13:49:50, 当前行数: 1

before upate ...
f1: 5, f2: number one, 时间: 2007-09-06 13:49:50, 当前行数: 1
after upate ... update condition: f1 = 5
f1: 5, f2: Olympiad in china, 时间: 2008-08-08 08:08:08, 当前行数: 1

你可能感兴趣的:(oracle,数据库,exception,Stream,delete,insert)