我正在尝试将我的C ++程序连接到Oracle数据库(12.2)。
我的C ++程序是(我在ubuntu中使用g ++编译器),
#include <occi.h>
#include <iostream>
using namespace std;
int main()
{ oracle::occi::Environment *env = oracle::occi::Environment::createEnvironment();
oracle::occi::Connection *conn = env->createConnection("bsk", "oraclepass");
env->terminateConnection(conn);
oracle::occi::Environment::terminateEnvironment(env);
}
我收到了错误
undefined reference to `oracle::occi::Environment::createEnvironment(oracle::occi::Environment::Mode, void*, void* (*)(void*, unsigned long), void* (*)(void*, void*, unsigned long), void (*)(void*, void*))'
Employees.cpp:(.text+0x169): undefined reference to `oracle::occi::Environment::terminateEnvironment(oracle::occi::Environment*)'
collect2: error: ld returned 1 exit status
我是数据库连接的新手。 你能帮帮我吗?
答案 0 :(得分:0)
从Oracle's网站,这是一个有效的例子:
const string userName = "HR";
const string password = "password";
const string connectString = "";
Environment *env = Environment::createEnvironment();
{
Connection *conn = env->createConnection(userName, password, connectString);
Statement *stmt = conn->createStatement("SELECT blobcol FROM mytable");
ResultSet *rs = stmt->executeQuery();
rs->next();
Blob b = rs->getBlob(1);
cout << "Length of BLOB : " << b.length();
...
stmt->closeResultSet(rs);
conn->terminateStatement(stmt);
env->terminateConnection(conn);
}
Environment::terminateEnvironment(env);
您的代码似乎正朝着正确的方向发展。所以,似乎缺少一些库!
同样,从different page on Oracle's website开始,这里是所需的库:
确保您的计算机上安装了这些库。
答案 1 :(得分:0)
改为尝试
$ g++ sample.C -I/opt/oracle/11.0.2/rdbms/public/ -L/opt/oracle/11.0.2/lib/ -locci -lclntsh
用您的Oracle主目录路径替换/opt/oracle/11.0.2
,它应该可以工作。