Red Hat Enterprise Linux AS4, C++ OCCI connect Oracle 9i
Red Hat Enterprise Linux AS4, C++ OCCI connect Oracle 9i,前提是已經安裝好Oracle 9i。
1. 下載對應的ORACLE client安裝。
http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html
因為沒有9i的client提供下載了,我選擇最接近的版本下載:
Version 10.1.0.5
Instant Client Package - Basic: All files required to run OCI, OCCI, and JDBC-OCI applications
Download instantclient-basic-linux32-10.1.0.5-20060511.zip (31,413,713 bytes) (cksum - 384697539)
下載之後解壓。
2. 先把範例程式碼給出來:
//oci.cpp
#include <iostream>
#include <string>
#include <occi.h>
using namespace std;
using namespace oracle::occi;
int main()
{
Connection *conn;
Environment *env;
Statement *stmt;
//build a connection
string struser("scott");
string strpwd("tiger");
string strconn_string("");
cout << "1" << endl;
env = Environment::createEnvironment(Environment::OBJECT);
cout << "2" << endl;
conn = env->createConnection(struser, strpwd, strconn_string);
cout << "3" << endl;
if( NULL != conn)
cout << "conn success" << endl;
else
cout << "conn failed" << endl;
//execute sql statement
string strsql("select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual");
stmt = conn->createStatement(strsql);
ResultSet *rset = stmt->executeQuery();
while(rset->next())
{
string time = rset->getString(1);
cout << "now time:" << time << endl;
}
//close resourses
stmt->closeResultSet(rset);
conn->terminateStatement(stmt);
env->terminateConnection(conn);
Environment::terminateEnvironment(env);
return 0;
}
3. 編譯
這裡對g++的版本有要求,如果是g++ 3.x.x的,可能會爆出如下錯誤:
undefined reference to `oracle::occi::Environment::createEnvironment(orac le::occi::Environment::Mode, void*, void* (*)(void*, unsigned int), void* (*)(void*, void*, unsigned int), void (*)(void*, void*))'
網上很多討論這個錯誤的,解決辦法是安裝低版本的g++。
--------------------------------------------------------------------------------
資料(可以不看):解決方案的出處:http://www.dbforums.com/showthread.php?1619980-Makefile-problem-for-OCCI-on-Oracle-9i
If you're getting the following error messages:
undefined reference to `oracle:occi::Environment::createEnvironment(oracle:occi::Environment::Mode, void*, void* (*)(void*, unsigned), void* (*)(void*, void*, unsigned), void (*)(void*, void*))'
and
undefined reference to `oracle:occi::Environment::terminateEnvironment(oracle:occi::Environment*)'
then you're probably running oracle9i on RedHat 8.0. to solve this, download and install the following rpm's from this redhat link:
* compat-gcc-7.3-2.96.118.i386.rpm
* compat-gcc-c++-7.3-2.96.118.i386.rpm
* compat-libstdc++-7.3-2.96.118.i386.rpm
* compat-libstdc++-devel-7.3-2.96.118.i386.rpm
and compile with g++296 instead of g++. sure you'll be stuck with g++-2.96, but at least it will compile and run.
--------------------------------------------------------------------------------
如下4個rpm包安裝:
* compat-gcc-7.3-2.96.118.i386.rpm
* compat-libstdc++-7.3-2.96.118.i386.rpm
* compat-libstdc++-devel-7.3-2.96.118.i386.rpm
* compat-gcc-c++-7.3-2.96.118.i386.rpm
在幫客之家1號FTP伺服器下載:
------------------------------------------分割線------------------------------------------
FTP地址:ftp://ftp1.bkjia.com
使用者名稱:ftp1.bkjia.com
密碼:www.bkjia.com
在 2016年LinuxIDC.com\3月\Red Hat Enterprise Linux AS4, C++ OCCI connect Oracle 9i\
下載方法見
------------------------------------------分割線------------------------------------------
請按照安裝上述順序安裝rpm包,因為最後一個依賴於第一二三個。
如果安裝的時候報錯說“正在安裝的g++與系統上存在的g++有衝突”,在rpm後加上-aid --force參數,如下:
sudo rpm -ivh xxx.rpm -aid --force
安裝好此版本的g++後,就可以用以下命令編譯了:
g++296 -o oci -I /opt/oracle/product/rdbms/public/ -I /opt/oracle/product/rdbms/demo/ -I /home/oracle/instantclient10_1/ -L /opt/oracle/product/lib/ -L /opt/oracle/product/rdbms/lib/ oci.cpp -lclntsh -locci /usr/lib/libstdc++.so.5 -Wall -O -g
注意仔細看上邊所包含的編譯目錄,先看看自己的$ORACLE_HOME在哪裡,看看上述的目錄都在不在。其中instantclient10_1目錄便是第一步下載的Oracle client。
4. 運行
結果如