Chinaunix首页 | 论坛 | 博客
  • 博客访问: 144686
  • 博文数量: 43
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-29 17:17
文章分类

全部博文(43)

文章存档

2017年(6)

2016年(13)

2015年(14)

2014年(10)

我的朋友

分类: Oracle

2015-09-08 00:11:52

原文地址:C++使用OCI连接Oracle 作者:davinciyxw

Oracle调用接口(Oracle Call Interface简称OCI)是Oracle数据库访问的基础接口。

下面的例子依次执行了:
  • 初始化OCI环境
  • 连接服务器
  • 连接数据库
  • 创建会话
  • 执行查询
  • 执行新增(更新,删除类似,不赘述)
  • 断开会话
  • 断开服务器
  • 释放资源
示例代码:

ociTest.cpp

点击(此处)折叠或打开

  1. #include <oci.h>
  2. #include <iostream>
  3. #include <string>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. using namespace std;

  7. //存放查询数据的结构体
  8. struct result
  9. {
  10.     char ename[20];
  11.     char cname[20];
  12.     result()
  13.     {
  14.         memset(ename, '\0', sizeof(ename));
  15.         memset(cname, '\0', sizeof(cname));
  16.     }
  17. };
  18.  
  19. int main()
  20. {
  21.     // 初始化 OCI 环境句柄指针
  22.     OCIEnv *envhpp = NULL;
  23.     // 初始化服务器句柄
  24.     OCIServer *servhpp = NULL;
  25.     // 用于捕获 OCI 错误信息
  26.     OCIError *errhpp = NULL;
  27.     // 初始化会话句柄
  28.     OCISession *usrhpp = NULL;
  29.     // 初始化服务上下文句柄
  30.     OCISvcCtx *svchpp = NULL;
  31.     // 初始化表达式句柄
  32.     OCIStmt *stmthpp = NULL;
  33.  
  34.     string server="mydb";
  35.  
  36.     // 创建 OCI 环境 , 并设置环境句柄。
  37.     sword swResult = OCIEnvCreate(&envhpp, OCI_DEFAULT, NULL, NULL, NULL, NULL, 0, NULL);
  38.     if (swResult != OCI_SUCCESS && swResult != OCI_SUCCESS_WITH_INFO)
  39.     {
  40.         cout << "Oracle environment initialization error!" << endl;
  41.         exit(1);
  42.     }
  43.     cout << "Oracle environment initialization success!" << endl;
  44.  
  45.     // 创建错误句柄
  46.     OCIHandleAlloc((dvoid *)envhpp, (dvoid **)&errhpp, OCI_HTYPE_ERROR, (size_t)0, (dvoid **)0);
  47.  
  48.     // 创建服务句柄
  49.     OCIHandleAlloc((dvoid *)envhpp, (dvoid **)&servhpp, OCI_HTYPE_SERVER, (size_t)0, (dvoid **)0);
  50.  
  51.     // 连接服务器,如果失败则获取错误码
  52.     if (OCIServerAttach(servhpp, errhpp, (text *)server.c_str(), strlen(server.c_str()), 0) != OCI_SUCCESS)
  53.     {
  54.         int errcno;
  55.         char errbuf[512] = "";
  56.         sb4 errcode;
  57.  
  58.         // 获取错误指针和 OCI 错误代码
  59.         OCIErrorGet((dvoid *)errhpp, (ub4)1, (text *)NULL, &errcode, (ub1 *)errbuf, (ub4)sizeof(errbuf), OCI_HTYPE_ERROR);
  60.         errcno = errcode;
  61.  
  62.         cout << "Oracle server attach error:" << errbuf << endl;
  63.         OCIHandleFree((dvoid *)envhpp,OCI_HTYPE_ENV);
  64.         OCIHandleFree((dvoid *)servhpp,OCI_HTYPE_SERVER);
  65.         OCIHandleFree((dvoid *)errhpp,OCI_HTYPE_ERROR);
  66.         exit(1);
  67.     }
  68.     cout << "Oracle server attach success!"<< endl;
  69.  
  70.     /***************** 连接数据库 ****************/
  71.     string user = "user";
  72.     string pas = "passwd";
  73.     errhpp = NULL;
  74.  
  75.     // 创建错误句柄
  76.     (void) OCIHandleAlloc((dvoid *)envhpp, (dvoid **)&errhpp, OCI_HTYPE_ERROR, (size_t)0, (dvoid **)0);
  77.     // 创建服务上下文句柄
  78.     (void) OCIHandleAlloc((dvoid *)envhpp, (dvoid **)&svchpp, OCI_HTYPE_SVCCTX, (size_t) 0, (dvoid **)0);
  79.     // 设置属性
  80.     (void) OCIAttrSet((dvoid *)svchpp, OCI_HTYPE_SVCCTX, (dvoid *)servhpp, (ub4)0, OCI_ATTR_SERVER, (OCIError *)errhpp);
  81.     // 创建用户连接句柄
  82.     (void) OCIHandleAlloc((dvoid *)envhpp, (dvoid **)&usrhpp, (ub4)OCI_HTYPE_SESSION, (size_t) 0, (dvoid **)0);
  83.     // 设置用户名、密码
  84.     (void) OCIAttrSet((dvoid *)usrhpp, (ub4)OCI_HTYPE_SESSION, (dvoid *)user.c_str(), (ub4)strlen(user.c_str()), (ub4)OCI_ATTR_USERNAME, errhpp);
  85.     (void) OCIAttrSet((dvoid *)usrhpp, (ub4)OCI_HTYPE_SESSION, (dvoid *)pas.c_str(), (ub4)strlen(pas.c_str()), (ub4)OCI_ATTR_PASSWORD, errhpp);
  86.  
  87.     // 创建会话连接
  88.     if(OCISessionBegin(svchpp, errhpp, usrhpp, OCI_CRED_RDBMS, (ub4)OCI_DEFAULT) != OCI_SUCCESS)
  89.     {
  90.         int errcno;
  91.         char errbuf[512]={'\0'};
  92.         sb4 errcode;
  93.         
  94.         // 获取错误指针和 OCI 错误代码
  95.         OCIErrorGet((dvoid *)errhpp, (ub4)1, (text *)NULL, &errcode, (ub1 *)errbuf, (ub4)sizeof(errbuf), OCI_HTYPE_ERROR);
  96.         errcno = errcode;
  97.         cout << "User session error:" << errbuf << endl;
  98.         OCIHandleFree((dvoid *)errhpp,OCI_HTYPE_ERROR);
  99.         OCIHandleFree((dvoid *)usrhpp,OCI_HTYPE_SESSION);
  100.         OCIHandleFree((dvoid *)svchpp,OCI_HTYPE_SVCCTX);
  101.         exit(1);
  102.     }
  103.     cout << "user session success!" << endl;
  104.     
  105.     (void) OCIAttrSet((dvoid *)svchpp, (ub4) OCI_HTYPE_SVCCTX, (dvoid *)usrhpp, (ub4)0, (ub4)OCI_ATTR_SESSION, errhpp);
  106.  
  107.     /*************** 执行 查询SQL 语句 ******************/
  108.     errhpp = NULL;
  109.  
  110.     // 创建一个表达式句柄
  111.     if (OCIHandleAlloc((dvoid *)envhpp, (dvoid **)&stmthpp, OCI_HTYPE_STMT, (size_t)0, (dvoid **)0) != OCI_SUCCESS)
  112.     {
  113.         cout << "Create STMT error !" << endl;
  114.         exit(1);
  115.     }
  116.     cout << "Create stmt success !" << endl;
  117.     
  118.     // 创建错误句柄
  119.     OCIHandleAlloc((dvoid *)envhpp, (dvoid **)&errhpp, OCI_HTYPE_ERROR, (size_t)0, (dvoid **)0);
  120.     
  121.     // Select语句
  122.     char sql[255] = "select col1, col2 from table1 ";
  123.     
  124.     if (OCIStmtPrepare(stmthpp, errhpp, (text *)sql, (ub4)strlen(sql), (ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT) != OCI_SUCCESS)
  125.     {
  126.          cout << "Create prepare error!" << sql << endl;
  127.          exit(1);
  128.     }
  129.     cout << "Create prepare success!" << endl;
  130.  
  131.     /********* 绑定参数 ***********/
  132.     // 申请绑定字段的句柄
  133.     OCIDefine *bhp1 = NULL;
  134.     OCIDefine *bhp2 = NULL;
  135.     
  136.     // 存放数据的结构
  137.     struct result rst;
  138.     
  139.     // 指定提取数据长度
  140.     ub2 datalen = 0;
  141.     // 定义指示器变量 , 用于取可能存在空值的字段
  142.     char isnul[6] = "";
  143.     // 定义输出变量 ,
  144.     OCIDefineByPos(stmthpp, &bhp1, errhpp, 1, (dvoid *)&rst.ename, sizeof(rst.ename), SQLT_CHR, NULL, &datalen, NULL, OCI_DEFAULT);
  145.     OCIDefineByPos(stmthpp, &bhp2, errhpp, 2, (dvoid *)&rst.cname, sizeof(rst.cname), SQLT_STR, NULL, &datalen, NULL, OCI_DEFAULT);
  146.  
  147.     // 获取 SQL 语句类型
  148.     ub2 stmt_type;
  149.     OCIAttrGet ((dvoid *)stmthpp, (ub4)OCI_HTYPE_STMT, (dvoid *)&stmt_type, (ub4 *)0, (ub4)OCI_ATTR_STMT_TYPE, errhpp);
  150.     
  151.     // 执行 SQL 语句
  152.     OCIStmtExecute(svchpp, stmthpp, errhpp, (ub4)0, (ub4)0, (OCISnapshot *)NULL, (OCISnapshot *)NULL, OCI_DEFAULT);
  153.  
  154.     // 获取查询信息
  155.     int rows_fetched;
  156.     do
  157.     {
  158.         cerr << rst.ename<< " ";
  159.         cerr << rst.cname<< " \n";
  160.     }
  161.     while(OCIStmtFetch2(stmthpp, errhpp, 1, OCI_FETCH_NEXT, 1, OCI_DEFAULT) != OCI_NO_DATA);
  162.  
  163.     // 获得记录条数
  164.     OCIAttrGet((CONST void *)stmthpp, OCI_HTYPE_STMT, (void *)&rows_fetched, (ub4 *)sizeof(rows_fetched), OCI_ATTR_ROW_COUNT, errhpp);
  165.     cout << " rows :" << rows_fetched << endl;
  166.  
  167.     /*************** 执行 新增SQL 语句 ******************/
  168.     if (OCIHandleAlloc((dvoid *)envhpp, (dvoid **)&stmthpp, OCI_HTYPE_STMT, (size_t)0, (dvoid **)0) != OCI_SUCCESS)
  169.     {
  170.         cout << "Create STMT error !" << endl;
  171.         exit(1);
  172.     }
  173.     cout << "Create stmt success !" << endl;
  174.  
  175.     OCIHandleAlloc((dvoid *)envhpp, (dvoid **)&errhpp, OCI_HTYPE_ERROR, (size_t)0, (dvoid **)0);
  176.  
  177.     // Insert语句
  178.     char sql2[255] = "insert into table1 (col1, col2) values('testoci', 'testoci')";
  179.     
  180.     // 准备Sql语句
  181.     if (OCIStmtPrepare(stmthpp, errhpp, (text *)sql2, (ub4)strlen(sql2), (ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT) != OCI_SUCCESS)
  182.     {
  183.          cout << "Create prepare error!" << sql2 << endl;
  184.          exit(1);
  185.     }
  186.     cout << "Create prepare success!" << endl;
  187.     
  188.     // 执行SQL 语句
  189.     OCIStmtExecute(svchpp, stmthpp, errhpp, (ub4)1, (ub4)0, (OCISnapshot *)NULL, (OCISnapshot *)NULL, OCI_DEFAULT);

  190.     // 断开用户会话
  191.     OCILogoff(svchpp, errhpp);
  192.     
  193.     // 断开服务器连接
  194.     OCIServerDetach(servhpp, errhpp, OCI_DEFAULT);
  195.     
  196.     // 释放资源
  197.     OCIHandleFree((dvoid *) stmthpp, OCI_HTYPE_STMT);
  198.     OCIHandleFree((dvoid *) svchpp, OCI_HTYPE_SVCCTX);
  199.     OCIHandleFree((dvoid *) servhpp, OCI_HTYPE_SERVER);
  200.     OCIHandleFree((dvoid *) errhpp, OCI_HTYPE_ERROR);
  201.  
  202.     return 0;
  203. }
在AIX 5.0环境下编译指令:
xlC -q64 -I/ora10g/app/oracle/product/10.2.0/rdbms/public -L/ora10g/app/oracle/product/10.2.0/lib -lclntsh  -o ociTest ociTest.cpp

执行:
./ociTest

输出:
Oracle environment initialization success!
Oracle server attach success!
user session success!
Create stmt success !
Create prepare success!
    
col1  col2  
 rows :1
Create stmt success !
Create prepare success!
阅读(1730) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~