OTL获取oracle数据库表格


     查了几天的资料,网上大部分都是些OTL库的讲解,实例少之又少,官方给的实例又太多,难以寻找自己需要的那一部分,

折腾折腾总算完成了基本的功能,提取oracle中的所有表格数据的测试,接下来就是整合优化了。

     首先去oracle官网下载两个oci相关的rpm,一个是库文件,另一个是头文件

    oracle-instantclient12.1-basic-12.1.0.2.0-1.x86_64.rpm

    oracle-instantclient12.1-devel-12.1.0.2.0-1.x86_64.rpm

   安装完成后在makefile中链接,

   库文件在/usr/lib/oracle/12.1/client64/lib中

   头文件在/usr/include/oracle/12.1/client64中

 

#include <iostream>
using namespace std;
#include <stdio.h>
#define OTL_ORA11G // Compile OTL 4/OCI8
#define OTL_STL//using std::string  
#define OTL_ORA_TIMESTAMP
#include "otlv4.h" 

otl_connect db;
void TypeString(string &sql)
{
    otl_stream in;
    in.open(100, //buffer size
        sql.c_str(),
        db // connectobject
        );
    while (!in.eof())
    {
        string data;
        in >> data;
        cout << data << endl;
    }
}

void TypeDouble(string &sql)
{
    otl_stream in;
    in.open(100, //buffer size
        sql.c_str(),
        db // connectobject
        );
    while (!in.eof())
    {
        double data;
        in >> data;
        cout << data << endl;
    }
}

void TypeFloat(string &sql)
{
    otl_stream in;
    in.open(100, //buffer size
        sql.c_str(),
        db // connectobject
        );
    while (!in.eof())
    {
        float data;
        in >> data;
        cout << data << endl;
    }
}
void TypeInt(string &sql)
{
    otl_stream in;
    in.open(100, //buffer size
        sql.c_str(),
        db // connectobject
        );
    while (!in.eof())
    {
        int data;
        in >> data;
        cout << data << endl;
    }
}

void TypeUnInt(string &sql)
{
    otl_stream in;
    in.open(100, //buffer size
        sql.c_str(),
        db // connectobject
        );
    while (!in.eof())
    {
        unsigned int data;
        in >> data;
        cout << data << endl;
    }
}
void TypeShort(string &sql)
{
    otl_stream in;
    in.open(100, //buffer size
        sql.c_str(),
        db // connectobject
        );
    while (!in.eof())
    {
        short data;
        in >> data;
        cout << data << endl;
    }
}

void TypeLongInt(string &sql)
{
    otl_stream in;
    in.open(100, //buffer size
        sql.c_str(),
        db // connectobject
        );
    while (!in.eof())
    {
        long int data;
        in >> data;
        cout << data << endl;
    }
}

void TypeTimestamp(string &sql)
{
    otl_stream in;
    in.open(100, //buffer size
        sql.c_str(),
        db // connectobject
        );
    while (!in.eof())
    {
        otl_datetime data;
        in >> data;
        cout << data.month << endl;
    }
}

void Select(string &table)
{
    string tablesql = "select * from " + table;
    string columnname;
    string sql;
    otl_stream i(100, // buffer size
        tablesql.c_str(),
        db // connect object
        );
    // create select stream

    otl_column_desc* desc;
    int desc_len;

    desc = i.describe_select(desc_len);
    for (int n = 0; n < desc_len; ++n)
    {
        cout << "the type is ==" << desc[n].dbtype << endl;
        cout << "otl_var_dbtype=" << desc[n].otl_var_dbtype << endl;
        cout << "the name is ==" << desc[n].name << endl;
        columnname = desc[n].name;
        sql = "select " + columnname + " from "+table;
        switch (desc[n].otl_var_dbtype)
        {
        case 1:
            TypeString(sql);
            break;
        case 2:
            TypeDouble(sql);
            break;
        case 3:
            TypeFloat(sql);
            break;
        case 4:
            TypeInt(sql);
            break;
        case 5:
            TypeUnInt(sql);
            break;
        case 6:
            TypeShort(sql);
            break;
        case 7:
            TypeLongInt(sql);
            break;
        case 8:
            TypeTimestamp(sql);
            break;
        }
        
    }
}

int main()
{
    int i = 1;
    otl_connect::otl_initialize(); // initialize OCI environment
    try{

          db.rlogon("user/password@(DESCRIPTION= (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.28.247)(PORT = 1521)) (CONNECT_DATA = (SERVER =DEDICATED) (SERVICE_NAME = PROTECT)))"); 
          if (db.connected)
          {
              cout << "right" << endl;
          }
          otl_stream o(100, // buffer size
              "select table_name from all_tables where owner='PROTECT'",
              db // connect object
              );
          string alltable;
          while (!o.eof())
          {
              o >> alltable;             
              cout << alltable.c_str() << endl;
              cout << "表格数目" << " " << i << endl;
              Select(alltable);
              i++;
          }
       }
     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;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值