OciDBI.cpp

来源:互联网 发布:sql delete select 编辑:程序博客网 时间:2024/06/04 23:22
#include "OciDBI.h"using namespace ocidbi;//test#include <iostream>#include <stdio.h>using namespace std;ocidbi::COciDBI::COciDBI(void){m_struser="";m_strpw="";m_strsid="";m_env = NULL;m_conn = NULL;m_pStm = NULL;}ocidbi::COciDBI::~COciDBI(void){if(m_conn)m_env->terminateConnection(m_conn);if(m_env)Environment::terminateEnvironment(m_env);}////////////////////////////////////////////////////////登录数据???//////////////////////////////////////////////////////bool ocidbi::COciDBI::LoginDB(const char *user,const char *pw,const char *sid){if(user == NULL || sid == NULL){m_strerr = "输入的用户、SID为空,请确认再链接!\n";return false;}try{m_env = Environment::createEnvironment();if (!m_env){m_strerr = "createEnvironment 初始化失???\n";return false;}m_conn =m_env->createConnection(user, pw , sid);if(!m_conn){if(m_env){Environment::terminateEnvironment(m_env);m_env = NULL;}m_strerr = "连接oracle服务端失???\n";return false;}}catch(SQLException ex){char chError[1024]={0};sprintf(chError,"Error Code:%d,Error Msg:%s",ex.getErrorCode(),ex.what());m_strerr = chError;if(m_conn){m_env->terminateConnection(m_conn);m_conn= NULL;}if(m_env){Environment::terminateEnvironment(m_env);m_env = NULL;}return false;}//记录用户、密码和SIDm_struser = user;m_strpw=pw;m_strsid=sid;return true;}///////////////////////////////////////////////////////////////执行有返回结果的查询//////////////////////////////////////////////////////////ResultSet *ocidbi::COciDBI::OpenQuery(const char *msql){if(msql==NULL){m_strerr="SQL语句为空!\n";return NULL;}try{m_pStm = m_conn->createStatement();//m_pStm->setPrefetchRowCount(1000);//m_pStm->setPrefetchMemorySize(1000000);//ResultSet *pRsTask = m_pStm->execute(msql);ResultSet *pRsTask = m_pStm->executeQuery(msql);return pRsTask;}catch(SQLException ex) {char chError[1024]={0};sprintf(chError,"Error Code:%d,Error Msg:%s",ex.getErrorCode(),ex.what());m_strerr = chError;}return NULL;}int64_t ocidbi::COciDBI::GetQueryNumber( const char *msql ){int64_t nSeq = -1;Statement * pstm =  m_conn->createStatement();        ResultSet *pRsTask = (pstm)->executeQuery(msql);if(pRsTask->next()){nSeq = pRsTask->getInt(1);}pstm->closeResultSet(pRsTask);m_conn->terminateStatement(pstm);return nSeq;}ResultSet *ocidbi::COciDBI::OpenQuery2(Statement** pstm, const char *msql){        if(msql==NULL){                m_strerr="SQL语?<8f>?为空!\n";                return NULL;        }        try{                *pstm = m_conn->createStatement();                ResultSet *pRsTask = (*pstm)->executeQuery(msql);                return pRsTask;        }        catch(SQLException ex) {                char chError[1024]={0};                sprintf(chError,"Error Code:%d,Error Msg:%s",ex.getErrorCode(),ex.what());                m_strerr = chError;        }        return NULL;}///////////////////////////////////////////////////////////关闭有返回结果的ResultSet指针///////////////////////////////////////////////////////bool ocidbi::COciDBI::CloseRecordSet(ResultSet *pRsTask){try{m_pStm->closeResultSet(pRsTask);m_conn->terminateStatement(m_pStm);}catch(SQLException ex) {char chError[1024]={0};sprintf(chError,"Error Code:%d,Error Msg:%s",ex.getErrorCode(),ex.what());m_strerr = chError;return false;}return true;}////////////////////////////////////////////////////////////////执行无返回结果的操作///////////////////////////////////////////////////////////bool ocidbi::COciDBI::Excute(const char *msql){try{Statement *pStResultTable = m_conn->createStatement();pStResultTable->execute(msql);m_conn->terminateStatement(pStResultTable);m_conn->commit();}catch(SQLException ex) {char chError[1024]={0};sprintf(chError,"Error Code:%d,Error Msg:%s",ex.getErrorCode(),ex.what());m_strerr = chError;return false;}return true;}string ocidbi::COciDBI::fn_GetErrMsg(){return m_strerr;}///////////////////////////////////////////////////关闭数据库连接,释放资源///////////////////////////////////////////////bool ocidbi::COciDBI::DisConnectDB(){try{m_env->terminateConnection(m_conn);m_conn = NULL;Environment::terminateEnvironment(m_env);m_env = NULL;}catch(SQLException ex) {char chError[1024]={0};sprintf(chError,"Error Code:%d,Error Msg:%s",ex.getErrorCode(),ex.what());m_strerr = chError;return false;}return true;}bool ocidbi::COciBTDBI::AddBtUserVersion(ClientVersion & version){bool nRet = true;try{char szsql[512]={0};sprintf(szsql,"insert into test(id,version) values(%d,'%s')",version.VersionID,version.strVersion.c_str());Excute(szsql);}catch(SQLException ex){char chError[1024]={0};sprintf(chError,"Error Code:%d,Error Msg:%s",ex.getErrorCode(),ex.what());m_strerr = chError;nRet = false;}return nRet;}//bool ocidbi::COciBTDBI::InitBtUserVersion(){//int blRet  = true;//try{//string sqlQuery = "SELECT  VersionID ,Description from DicVersion";//Statement *stmt = m_conn->createStatement (sqlQuery);//ResultSet *rset = stmt->executeQuery ();//while (rset->next ()){// uint32 VersionID = rset->getInt(1);// string strVersion = rset->getString(2);// printf("%d  %s \n",VersionID,strVersion.c_str());//}//    stmt->closeResultSet (rset);//   m_conn->terminateStatement (stmt);//}//catch(SQLException ex){//char chError[1024]={0};//sprintf(chError,"Error Code:%d,Error Msg:%s",ex.getErrorCode(),ex.what());//m_strerr = chError;//blRet =false;//}//return blRet;//}bool ocidbi::COciBTDBI::InitBtUserVersion(){int blRet  = true;try{string sqlQuery = "SELECT  id1  from testaaa";Statement *stmt = m_conn->createStatement (sqlQuery);ResultSet *rset = stmt->executeQuery ();while (rset->next ()){uint64  number1 =  rset->getInt(1);/*uint32 VersionID = rset->getInt(1);string strVersion = rset->getString(2);*/printf("%llu\n",number1);}stmt->closeResultSet (rset);m_conn->terminateStatement (stmt);}catch(SQLException ex){char chError[1024]={0};sprintf(chError,"Error Code:%d,Error Msg:%s",ex.getErrorCode(),ex.what());m_strerr = chError;blRet =false;}return blRet;}bool ocidbi::COciBTDBI::AddBtUserVersion(string strnumber){bool nRet = true;try{char szsql[512]={0};sprintf(szsql,"insert into testaaa(id1) values('%s')",strnumber.c_str());Excute(szsql);}catch(SQLException ex){char chError[1024]={0};sprintf(chError,"Error Code:%d,Error Msg:%s",ex.getErrorCode(),ex.what());m_strerr = chError;nRet = false;}return nRet;}
0 0