所需软件包如下:
instantclient-basic-linux32-11.2.0.2.0.zip
instantclient-sdk-linux32-11.2.0.2.0.zip
安装:
1. 将以上两个文件解压到/opt文件夹下:
unzip instantclient-basic-linux32-11.2.0.2.0.zip
unzip instantclient-sdk-linux32-11.2.0.2.0.zip
mv instantclient_11_2 /opt/
cd /opt/instantclient_11_2/
(做两个必须的链接)
ln -s libocci.so.11.1 libocci.so
ln -s libclntsh.so.11.1 libclntsh.so
2. 添加环境变量
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/instantclient_11_2
export LD_LIBRARY_PATH
CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:/opt/instantclient_11_2/sdk/include
export CPLUS_INCLUDE_PATH
TNS_ADMIN=/opt/instantclient_11_2
export TNS_ADMIN
3. 在/opt/instantclient_11_2下添加tnsnames.ora文件,内容如下:
XE =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.110)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = XE)
)
)
(以上内容不详细说明,具体可参见oracle文档)
4. 编写程序测试一下:
//code
#include
#include
#include
void DisplayAllRows(oracle::occi::Connection* conn)
{
std::string sqlStmt = "SELECT USER_ID, NAME FROM IM_USER";
oracle::occi::Statement *stmt;
oracle::occi::ResultSet *rset;
stmt = conn->createStatement( sqlStmt );
try {
rset = stmt->executeQuery();
while ( rset->next() ) {
std::cout
<< "USER_ID: " << rset->getString(1) << " "
<< "NAME: " << rset->getString(2) << std::endl;
}
} catch(oracle::occi::SQLException ex) {
std::cout << "Exception thrown for displayAllRows" << std::endl;
std::cout << "Error number: " << ex.getErrorCode() << std::endl;
std::cout << ex.getMessage() << std::endl;
}
stmt->closeResultSet(rset);
conn->terminateStatement(stmt);
}
int
main(int argc, char **argv)
{
oracle::occi::Environment *env;
oracle::occi::Connection *conn;
std::string user = "im";
std::string passwd = "123456";
std::string connect_string = "XE"; //与tnsnames文件中的名字一致
try {
env = oracle::occi::Environment::createEnvironment(oracle::occi::Environment::DEFAULT);
conn = env->createConnection(user, passwd, connect_string);
DisplayAllRows(conn);
env->terminateConnection(conn);
oracle::occi::Environment::terminateEnvironment(env);
} catch (oracle::occi::SQLException ea) {
std::cout << "Error: " << ea.getMessage() << std::endl;
}
return 0;
}
编译:
g++ -o occi_dml occi_dml.cc -L/opt/instantclient_11_2 -locci -lclntsh
如果没报错的话,就恭喜你了,哈哈.......
阅读(5256) | 评论(0) | 转发(0) |