设为首页 加入收藏

TOP

C++通过OCCI操作Oracle数据库(一)
2014-04-06 17:34:38 来源: 作者: 【 】 浏览:194
Tags:通过 OCCI 操作 Oracle 数据库

  1.安装OCCI

  如果oracle数据库默认没有安装OCCI,可以自己从官网上下载与自己数据库版本一致的API,其中包含四个软件包:

  oracle-instantclient-sqlplus-10.2.0.5-1.i386.rpm

  oracle-instantclient-devel-10.2.0.5-1.i386.rpm

  oracle-instantclient-odbc-10.2.0.5-1.i386.rpm

  oracle-instantclient-basic-10.2.0.5-1.i386.rpm

  安装完成之后,会在/usr/lib下多个oracle 共享库文件夹,在/usr/include下多个oracle 头文件(接口)文件夹(可以将他们放到环境变量中)。我的数据库版本是10.2.0,下面以此为例。

  2.编写HelloWorld程序测试连接

  #include <iostream>

  #define LINUXOCCI //避免函数重定义错误

  #include <occi.h>

  using namespace std;

  using namespace oracle::occi;

  int main()

  {

  Environment *env=Environment::createEnvironment(Environment::DEFAULT);

  cout《"success"《endl;

  string name = "scott";

  string pass = "tiger";

  string srvName = "127.0.0.1:1522/orcl";

  try

  {

  Connection *conn = env->createConnection(name, pass);

  cout《"conn success"《endl;

  env->terminateConnection(conn);

  }

  catch(SQLException e)

  {

  cout《e.what()《endl;

  return -1;

  }

  Environment::terminateEnvironment(env);

  cout《"end!"《endl;

  return 0;

  }

  编译命令:

  g++

  test.cc -o test -I/usr/include/oracle/10.2.0.5/client -L/usr/lib/oracle/10.2.0.5/client/lib -locci -lsqlplus

  我没有将occi的路径加入到环境变量中,所以此处要显示列出目录才能通过编译,找到共享库。

  运行。/test会报错,libocci.so找不到,解决办法很简单:将/usr/lib/oracle/…/lib下的库加入到LD_LIBRARY_PATH中就可以了。

  输出结果:

  success

  conn

  success

  end!

  注:这件不幸的事情可能只发生在我身上了,本人系统中有三个用户,其中一个是oracle,而程序是用另一个用户写的,于是编译通过了,但是运行总是报错:

  ORA-12162:

  TNS:net service name is incorrectly specified

  后来查明,这个是由于没有设置并导出ORACLE_SID.换了oracle用户试试,居然运行通过了,真的很伤心,原来那个用户没有设置Oracle环境变脸怎么能直接本地访问呢。

  3.进行一些操作,执行sql语句

  Employees.h

  /*

  * A simple OCCI test application

  * This file contains the Employees class declaration

  */

  #include <iostream>

  #include <occi.h>

  #include <iomanip>

  using namespace oracle::occi;

  using namespace std;

  class Employees {

  public:

  Employees();

  virtual ~Employees();

  void List();

  private:

  Environment *env;

  Connection  *con;

  string user;

  string passwd;

  string db;

  };

  Employees::Employees()

  {

  /*

  69    * connect to the test database as the HR

  70    * sample user and use the EZCONNECT method

  71    * of specifying the connect string. Be sure

  72    * to adjust for your environment! The format

  73    * of the string is host:port/service_name

  74 */

  user = "scott";

  passwd = "tiger";

  db = "127.0.0.1:1522/orcl";

  env = Environment::createEnvironment(Environment::DEFAULT);

  try

  {

  con = env->createConnection(user, passwd, db);

  }

  catch (SQLException& ex)

  {

  cout 《 ex.getMessage();

  exit(EXIT_FAILURE);

  }

  }

  Employees::~Employees()

  {

  env->terminateConnection (con);

  Environment::terminateEnvironment (env);

  }

   

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇LRU缓存算法 - C++版 下一篇windows下的C++ socket服务器

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·HTTPS 详解一:附带 (2025-12-26 02:20:37)
·TCP/IP协议到底在讲 (2025-12-26 02:20:34)
·TCP和UDP在socket编 (2025-12-26 02:20:32)
·有没有适合新手练习 (2025-12-26 01:48:47)
·用清华镜像网怎么下 (2025-12-26 01:48:44)