用C++操作ORACLE的例子

changleqy 2008-07-17 11:41:47
有没有朋友能提供C++操作ORACLE数据库的例子,
ADO,ODBC等方法都行。谢谢!
...全文
195 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
tdjdyq 2008-07-17
  • 打赏
  • 举报
回复
我刚下了一个
hurryboylqs 2008-07-17
  • 打赏
  • 举报
回复
大把,自己google下
tdjdyq 2008-07-17
  • 打赏
  • 举报
回复
我刚写了一个
oracle.h
#ifndef _ORACLE_H_
#define _ORACLE_H_

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <oci.h>

#include <string>
#include <windows.h>
#include <iostream>

using namespace std;



#ifdef _WIN32
#define snprintf _snprintf
#endif

#define NOT_BIND 0
#define ALREADY_BIND 1

typedef struct sjw_oci
{
char pIp[32];
int nPort;
char pUser[32];
char pPass[32];
char pSid[32];
OCIEnv *envhp;
OCIError *errhp;
OCIServer *srvhp;
OCISvcCtx *svchp;
OCISession *authp;
OCIStmt *stmthp;
OCIBind *bindhp;
OCIParam *colhp;
OCIDefine **defhp;
}CDbConnection;

typedef struct sjw_data
{
int nTag;
long nNumColumn;
long nNumRow;
char** pBuffer;
int* pBuferLen;
}CDbData;

enum sjw_sentence
{
SJW_SELETE=0,
SJW_INSERT,
SJW_DELETE,
SJW_UPDATE,
SJW_COMMIT,
SJW_ROLLBACK
};


/*
大写字母变小写
*/
string UpperToLower(const string&);

class COracle
{
private:
CDbConnection *m_DbConnection;
CDbData *m_DbData;
char m_pCmd[65535];
bool m_bConnection;
int m_nSentence;
private:
int DbInit();
int DbFree();
public:
int DbConnect();
int DbExecute();
bool DbFetch();
int DbCommit();
int Disconnect();
int DbRollBack();
int GetDbError(string& strError);
void SetDbPara(const char*,const char*,const char*,const char*,int);
void SetCmd(char*);
string GetData(int);
COracle();
~COracle();
};

#endif //_ORACLE_H__

tdjdyq 2008-07-17
  • 打赏
  • 举报
回复
#include "oracle.h"
string UpperToLower(const string& str)
{
}
COracle::COracle()
{
m_bConnection = false;
m_nSentence = -1;
int nPort = 0;
memset(m_pCmd,0,sizeof(m_pCmd));
}
COracle::~COracle()
{
Disconnect();
}
int COracle::DbInit()
{
m_DbConnection = (CDbConnection *)malloc(sizeof(CDbConnection));
if(m_DbConnection == NULL)return 1;
m_DbData = (CDbData *)malloc(sizeof(CDbData));
if(m_DbData == NULL)return 1;
m_DbData->nTag = NOT_BIND;
memset(m_DbConnection, 0, sizeof(CDbConnection));
memset(m_DbConnection->pIp,0,sizeof(m_DbConnection->pIp));
memset(m_DbConnection->pUser,0,sizeof(m_DbConnection->pUser));
memset(m_DbConnection->pSid,0,sizeof(m_DbConnection->pSid));
return 0;
}
void COracle::SetDbPara(const char* ip,const char* user,const char* pass,const char* sid,int port)
{
if(DbInit()) return;
if(m_DbConnection == NULL) return;
_snprintf(m_DbConnection->pIp, sizeof(m_DbConnection->pIp), "%s", ip);
_snprintf(m_DbConnection->pUser, sizeof(m_DbConnection->pUser), "%s", user);
_snprintf(m_DbConnection->pPass, sizeof(m_DbConnection->pPass), "%s", pass);
_snprintf(m_DbConnection->pSid, sizeof(m_DbConnection->pSid), "%s", sid);
m_DbConnection->nPort = port;
}
void COracle::SetCmd(char* pCmd)
{
char pKey[64];
memset(pKey,0,sizeof(pKey));
if(pCmd == NULL) return;
char* pStr = pCmd;
while((*pStr == 0x20 || *pStr == 0x09) && *pStr != 0x00) pStr++;
char* pEnd = pStr;
while((*pEnd != 0x20 && *pEnd != 0x09) && *pEnd != 0x00) pEnd++;
memcpy(pKey,pStr,pEnd - pStr);
string str = UpperToLower(pKey);
if(str == "select") m_nSentence = SJW_SELETE;
else if(str == "insert") m_nSentence = SJW_INSERT;
else if(str == "delete") m_nSentence = SJW_DELETE;
else if(str == "update") m_nSentence = SJW_UPDATE;
else if(str == "commit") m_nSentence = SJW_COMMIT;
else if(str == "rollback") m_nSentence = SJW_ROLLBACK;
else
{
m_nSentence = -2;Sleep(10000);return;
}
_snprintf(m_pCmd, sizeof(m_pCmd), "%s", pCmd);
}
int COracle::DbConnect()
{
OCIEnv *envhp;
OCIError *errhp;
OCISvcCtx *svchp;
OCIStmt *stmthp;
if(m_DbConnection==NULL) return 1;
if(OCIInitialize((ub4) OCI_DEFAULT, (dvoid *)0, (dvoid * (*)(dvoid *, size_t))0, (dvoid * (*)(dvoid *, dvoid *, size_t))0, (void (*)(dvoid *, dvoid *)) 0))
return 1;
if(OCIEnvInit((OCIEnv **) &envhp, OCI_DEFAULT, (size_t) 0, (dvoid **) 0)) return 1;
if(OCIHandleAlloc((dvoid *) envhp, (dvoid **) &errhp, OCI_HTYPE_ERROR,(size_t) 0,(dvoid **) 0)) return 1;
if(OCIHandleAlloc((dvoid *) envhp, (dvoid **)&svchp, OCI_HTYPE_SVCCTX,(size_t)NULL, (dvoid **)NULL))
{
OCIHandleFree((dvoid *) errhp, OCI_HTYPE_ERROR); OCIHandleFree((dvoid *) envhp, OCI_HTYPE_ENV); return 1;
}
if(OCILogon(envhp, errhp, &svchp, (const OraText*)m_DbConnection->pUser, strlen(m_DbConnection->pUser), (const OraText*)m_DbConnection->pPass, strlen(m_DbConnection->pPass),(const OraText*)m_DbConnection->pSid, strlen(m_DbConnection->pSid)))
{
OCIHandleFree((dvoid *) svchp, OCI_HTYPE_SVCCTX); OCIHandleFree((dvoid *) errhp, OCI_HTYPE_ERROR); OCIHandleFree((dvoid *) envhp, OCI_HTYPE_ENV); return 1;
}
if(OCIHandleAlloc((dvoid *)envhp, (dvoid **)&stmthp, OCI_HTYPE_STMT,(size_t)NULL, (dvoid **)NULL))
{
OCILogoff(svchp, errhp); OCIHandleFree((dvoid *) svchp, OCI_HTYPE_SVCCTX); OCIHandleFree((dvoid *) errhp, OCI_HTYPE_ERROR); OCIHandleFree((dvoid *) envhp, OCI_HTYPE_ENV); return 1;
}
m_DbConnection->envhp = envhp;
m_DbConnection->errhp = errhp;
m_DbConnection->svchp = svchp;
m_DbConnection->stmthp = stmthp;
m_DbConnection->defhp = NULL;
m_DbConnection->bindhp = NULL;
return 0;
}
int COracle::DbExecute()
{
if(m_nSentence == SJW_SELETE)
{
int num_col;
char**buf_col;
int*len_col;
int i;
OCIParam* colhp;
OCIDefine** defhp;
char tmpbuf[7][50];
memset(tmpbuf, 0, 350);
if(m_DbConnection == NULL || m_pCmd == NULL || m_DbData == NULL)
return 1;
if(m_DbData->nTag== NOT_BIND)
{
num_col = 1;
if(OCIStmtPrepare(m_DbConnection->stmthp, m_DbConnection->errhp, (const OraText*)m_pCmd, (ub4)strlen(m_pCmd), OCI_NTV_SYNTAX, OCI_DEFAULT) > 0)
return 1;
if(OCIStmtExecute(m_DbConnection->svchp, m_DbConnection->stmthp, m_DbConnection->errhp, (ub4) 0, (ub4) 0,(CONST OCISnapshot *) NULL,(OCISnapshot *) NULL, OCI_DESCRIBE_ONLY) > 0)
return 1;
if(OCIAttrGet(m_DbConnection->stmthp, OCI_HTYPE_STMT, &num_col, 0, OCI_ATTR_PARAM_COUNT, m_DbConnection->errhp))
return 1;
if(num_col == 0) return 1;
len_col = (int *)malloc(sizeof(int) * num_col);
if(len_col == NULL) return 1;
buf_col = (char **)malloc(sizeof(char *) * num_col);
if(buf_col == NULL) return 1;
defhp = (OCIDefine **)malloc(sizeof(OCIDefine *) * num_col);
if(defhp == NULL) return 1;
m_DbData->nNumColumn = num_col;
m_DbData->nNumRow = 0;
m_DbData->pBuffer = buf_col;
m_DbData->pBuferLen = len_col;

memset(len_col, 0, sizeof(int) * num_col);
memset(buf_col, 0, sizeof(char *) * num_col);
for(i=1; i<=num_col; i++)
{
OCIParamGet(m_DbConnection->stmthp, OCI_HTYPE_STMT, m_DbConnection->errhp, (void **)&colhp, i);
OCIAttrGet(colhp, OCI_DTYPE_PARAM, (len_col+i-1), 0, OCI_ATTR_DATA_SIZE, m_DbConnection->errhp);
*(len_col+i-1) = *(len_col+i-1)+1;
*(buf_col+i-1) = (char *)malloc((int)(*(len_col+i-1)));
if(*(buf_col+i-1) == NULL)
return 1;
memset(*(buf_col+i-1), 0, (int)(*(len_col+i-1)));
if(OCIDefineByPos(m_DbConnection->stmthp, (defhp+i-1), m_DbConnection->errhp, i, (ub1 *)(*(buf_col+i-1)), *(len_col+i-1), SQLT_STR, NULL, (ub2 *)0, (ub2 *)0, OCI_DEFAULT))
return 1;
}
if (OCIStmtExecute(m_DbConnection->svchp, m_DbConnection->stmthp, m_DbConnection->errhp, (ub4) 0, (ub4) 0,(CONST OCISnapshot *) NULL,(OCISnapshot *) NULL, OCI_DEFAULT) > 0)
return 1;
m_DbData->nTag = ALREADY_BIND;
}
for(i=0; i<m_DbData->nNumColumn; i++) memset(m_DbData->pBuffer[i], 0, m_DbData->pBuferLen[i]);
}
else
{
if(OCIStmtPrepare(m_DbConnection->stmthp, m_DbConnection->errhp, (const OraText*)m_pCmd, (ub4)strlen(m_pCmd), OCI_NTV_SYNTAX, OCI_DEFAULT))
return 1;
if(OCIStmtExecute(m_DbConnection->svchp, m_DbConnection->stmthp, m_DbConnection->errhp, (ub4) 1, (ub4) 0,(CONST OCISnapshot *) NULL,(OCISnapshot *) NULL, OCI_DEFAULT | OCI_COMMIT_ON_SUCCESS))
return 1;
}
return 0;
}

bool COracle::DbFetch()
{
if(OCI_NO_DATA != OCIStmtFetch(m_DbConnection->stmthp, m_DbConnection->errhp, 1, OCI_FETCH_NEXT, OCI_DEFAULT))
{ m_DbData->nNumRow++; return 0;}
else return 1;
}


int COracle::DbCommit()
{
if(OCITransCommit(m_DbConnection->svchp, m_DbConnection->errhp, (ub4) 0))return 1;
return 0;
}

int COracle::Disconnect()
{
DbFree();OCILogoff(m_DbConnection->svchp, m_DbConnection->errhp); OCIHandleFree((dvoid *) m_DbConnection->svchp, OCI_HTYPE_SVCCTX);
OCIHandleFree((dvoid *) m_DbConnection->errhp, OCI_HTYPE_ERROR);
OCIHandleFree((dvoid *) m_DbConnection->envhp, OCI_HTYPE_ENV);
return 0;
}
int COracle::DbRollBack()
{
if(OCITransRollback(m_DbConnection->svchp, m_DbConnection->errhp, (ub4) 0))return 1;
return 0;
}
int COracle::GetDbError(string& str)
{
sb4 uErrorCode;
unsigned char acDiscription[1024] = "";
OCIErrorGet(m_DbConnection->errhp,1,NULL,&uErrorCode, acDiscription, 1024-1,OCI_HTYPE_ERROR);
str = (char*)acDiscription;
return uErrorCode;
}
string COracle::GetData(int nIndex)
{
string str;
if(nIndex >= m_DbData->nNumColumn) return "";
return str;
}

int COracle::DbFree()
{
int i;
if(m_DbData == NULL) return 1;
if(m_DbData->pBuferLen) free(m_DbData->pBuferLen);
if(m_DbData->pBuffer)
{
for(i=0;i<m_DbData->nNumColumn;i++)
{
if(*(m_DbData->pBuffer+i))
free(*(m_DbData->pBuffer+i));
}
free(m_DbData->pBuffer);
}
return 0;
}

16,466

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧