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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章