首页 > 其他 > 详细

OCI编程之个人浅见(二)

时间:2015-12-24 23:46:52      阅读:328      评论:0      收藏:0      [点我收藏+]

直接贴出代码:

  1 #include <cstdio>
  2 #include <cstdlib>
  3 #include <cstring>
  4 #include "oci.h"
  5 #include <sstream>
  6 #include <iostream>
  7 
  8 using namespace std;
  9 
 10 
 11 typedef struct TExecInfo
 12 {
 13     OCIEnv *envhp;
 14     OCIError *errhp;
 15     OCISession *authp = (OCISession *)0;
 16     OCIServer *srvhp;
 17     OCISvcCtx *svchp;
 18     OCIStmt   *stmt;
 19     OCIDefine *defnp = (OCIDefine *)0;
 20     OCIBind  *bnd1p = (OCIBind *)0;
 21 }TExecInfo;
 22 
 23 typedef struct ResultSet
 24 {
 25     int f_int1;
 26     int f_int2;
 27     int f_int3;
 28     int f_int4;
 29 }ResultSet;
 30 
 31 void* connect(TExecInfo *execInfo, string username, string passwd, string dburl)
 32 {
 33     int errCode = 0;
 34     errCode = OCIEnvCreate((OCIEnv **)&execInfo->envhp, (ub4)OCI_DEFAULT,
 35         (void *)0, (void * (*)(void *, size_t)) 0,
 36         (void * (*)(void *, void *, size_t)) 0,
 37         (void(*)(void *, void *)) 0, (size_t)0, (void **)0);
 38     if (errCode != 0)
 39     {
 40         cout << "OCIEnvCreate failed with errcode = " << errCode << endl;
 41         exit(1);
 42     }
 43     (void)OCIHandleAlloc((void *)execInfo->envhp, (void **)&execInfo->errhp, OCI_HTYPE_ERROR,
 44         (size_t)0, (void **)0);
 45 
 46     (void)OCIHandleAlloc((void *)execInfo->envhp, (void **)&execInfo->srvhp, OCI_HTYPE_SERVER,
 47         (size_t)0, (void **)0);
 48 
 49     (void)OCIHandleAlloc((void *)execInfo->envhp, (void **)&execInfo->svchp, OCI_HTYPE_SVCCTX,
 50         (size_t)0, (void **)0);
 51 
 52     text errbuf[512];
 53     errCode = OCIServerAttach(execInfo->srvhp, execInfo->errhp, (unsigned char *)dburl.c_str(), dburl.length(), OCI_DEFAULT);
 54     if (errCode != OCI_SUCCESS)
 55     {
 56         cout << "OCIServerAttach failed!" << endl;
 57         (void) OCIErrorGet((void *)execInfo->errhp, (ub4) 1, (text *) NULL, &errCode,
 58                         errbuf, (ub4) sizeof(errbuf), OCI_HTYPE_ERROR);
 59         (void) printf("Error - %.*s\n", 512, errbuf);
 60     }
 61 
 62     (void)OCIAttrSet((void *)execInfo->svchp, OCI_HTYPE_SVCCTX, (void *)execInfo->srvhp, (ub4)0, OCI_ATTR_SERVER, (OCIError *)execInfo->errhp);
 63 
 64     (void)OCIHandleAlloc((void *)execInfo->envhp, (void **)&execInfo->authp,
 65         (ub4)OCI_HTYPE_SESSION, (size_t)0, (void **)0);
 66 
 67     (void)OCIAttrSet((void *)execInfo->authp, OCI_HTYPE_SESSION, (text *)username.c_str(), (ub4)strlen((char *)username.c_str()), (ub4)OCI_ATTR_USERNAME, execInfo->errhp);
 68 
 69     (void)OCIAttrSet((void *)execInfo->authp, OCI_HTYPE_SESSION, (text *)passwd.c_str(), (ub4)strlen((char *)passwd.c_str()), (ub4)OCI_ATTR_PASSWORD, execInfo->errhp);
 70 
 71     errCode = OCISessionBegin(execInfo->svchp, execInfo->errhp, execInfo->authp, OCI_CRED_RDBMS, (ub4)OCI_DEFAULT);
 72     if (errCode != OCI_SUCCESS)
 73     {
 74         cout << "OCISessionBegin failed!" << endl;
 75     }
 76 
 77 
 78     (void)OCIAttrSet((void *)execInfo->svchp, (ub4)OCI_HTYPE_SVCCTX, (void *)execInfo->authp, (ub4)0, (ub4)OCI_ATTR_SESSION, execInfo->errhp);
 79 
 80 }
 81 
 82 void* prepare(TExecInfo *execInfo, string sqlStr)
 83 {
 84     int errCode = 0;
 85     OCIHandleAlloc((void *)execInfo->envhp, (void **)&execInfo->stmt, OCI_HTYPE_STMT, (size_t)0, (void **)0);
 86 
 87     OCIHandleAlloc((void *)execInfo->envhp, (void **)&execInfo->stmt, OCI_HTYPE_STMT, (size_t)0, (void **)0);
 88 
 89     /* Retrieve the current maximum employee number. */
 90     errCode = OCIStmtPrepare(execInfo->stmt, execInfo->errhp, (text *)sqlStr.c_str(), strlen(sqlStr.c_str()), OCI_NTV_SYNTAX, OCI_DEFAULT);
 91     if (errCode != OCI_SUCCESS)
 92     {
 93         cout << "OCIStmtPrepare failed!" << endl;
 94     }
 95 }
 96 
 97 
 98 
 99 
100 
101 int main()
102 {
103     TExecInfo *execInfo = new TExecInfo;
104     string username = "xxx";
105     string passwd = "*****";
106     string dburl = "ip:config/sid";
107     string sqlStr = "select * from t1";
108     connect(execInfo, username, passwd, dburl);
109     prepare(execInfo, sqlStr);
110     int iRet = OCIStmtExecute(execInfo->svchp, execInfo->stmt, execInfo->errhp, (ub4)0, (ub4)0, (CONST OCISnapshot *) NULL, (OCISnapshot *)NULL, OCI_DEFAULT);
111     if (iRet != OCI_SUCCESS)
112     {
113         cout << "execute failed!" << endl;
114     }
115     
116 
117 
118     cout << "execute success!" << endl;
119 
120 
121 
122 }

有点累,明天再写

 

OCI编程之个人浅见(二)

原文:http://www.cnblogs.com/night-dim-light/p/5074514.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!