国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

OCI 調用Oracle 存儲過程實現

2019-11-09 13:32:35
字體:
來源:轉載
供稿:網友

OCI調用存儲過程 最近的風控框架開發中,遇到一個場景,即程序需要調用Oracle中的存儲過程,但是目前的框架中并不支持,只支持調用組裝好的SQL語句,即基本的數據庫增刪改查操作,這樣就需要開發OCI調用存儲過程。 基于這個需求,開始網上找尋資料。由于使用到的是OCI接口,而該接口文檔只有英文版,而且很不全面,偏偏需要使用到的OCI調用存儲過程API程序例子沒有,所以只能從網上一些博客等找尋。經過不停的搜索,關鍵字換來換去的搜索,搜索引擎從百度,Google鏡像網站,最后到微軟的必應搜索,都使用到了,仍然沒有搜索到一個正確的完整的代碼示例。找到了一兩個類似的感覺可以使用的,編譯,運行發現都是不行的。驗證了那就話,中國的很多技術博客就是你抄我,我抄你。然后開始往一些國外的網站搜索,StackOverflow等等,都沒有搜索到完整的正確的示例,最后根據之前的OCI程序封裝,推測出調用存儲過程的類似寫法,把整體的調用函數寫完了,之后就是參數的問題了,參數比較多,使用起來特別復雜,需要知道每個參數的意義。經過不斷的編譯,運行,測試,最后將所有的參數調試正確運行完整。 最后的完整示例代碼如下,方便以后的開發人員使用。不用像我這樣發費這么大精力去搜索和試驗。

int ora_PRocedure_execute(structS_OracleContext*oractx, constchar*pszSQL,char*userid,char*exptype,char*conditionexp,char**resultof,int*errorcode,char**errormsg){ OCIBind * bidhp[6]; sb2 sb2aInd[3]; OCIStmt*stmthp=NULL; sWordretcode = 0; sb4errcode = 0; *resultof= NULL; *errorcode=NULL; *errormsg= NULL; char *pszGBKSQL; //數據庫需要 GBK的SQL語句。因此需要轉換字符集 pszGBKSQL= UTF8ToGBK(pszSQL); if (!pszGBKSQL) { LogMessage("utf8 to gbk failed in ora_execute()"); return -1; } if (NULL ==oractx->envhp) { LogMessage("oractx->envhp==NULL inora_execute()."); delete pszGBKSQL; return -2; } retcode= OCIHandleAlloc( (dvoid *) oractx->envhp, (dvoid **) &stmthp,OCI_HTYPE_STMT, (size_t) 0, (dvoid **) 0); ora_check_error(oractx->errhp,retcode,&errcode); if (0!=retcode || NULL==stmthp) { LogMessage("OCIHandleAlloc()failed in ora_execute()."); delete pszGBKSQL; return -3; } retcode= OCIStmtPrepare(stmthp, oractx->errhp, (text *)pszGBKSQL, (ub4) strlen(pszGBKSQL), (ub4)OCI_NTV_SYNTAX, (ub4) OCI_DEFAULT); ora_check_error(oractx->errhp,retcode, &errcode); if(retcode!=OCI_SUCCESS&& retcode!=OCI_SUCCESS_WITH_INFO) { LogMessage("OCIStmtPrepare() failed %d,%d. reconnect ORACLE,sql=%s", retcode,errcode,pszGBKSQL); if(errcode != 1756) { OCIHandleFree(stmthp,OCI_HTYPE_STMT); ora_clean(oractx); delete pszGBKSQL; return -4; } LogMessage("Error 1756, minor error, ignore it."); OCIHandleFree(stmthp,OCI_HTYPE_STMT); delete pszGBKSQL; return 0; } //綁定3個輸入變量 if ((retcode=OCIBindByPos(stmthp, &bidhp[0],oractx->errhp,(ub4) 1, (dvoid *)userid,(sb4)(strlen(userid)+1),SQLT_STR,NULL, NULL, NULL, 0, NULL, (ub4)OCI_DEFAULT))!=OCI_SUCCESS) { ora_check_error(oractx->errhp,retcode, &errcode); LogMessage("ora_procedure_execute Error when OCIBindByPosuserid/n"); return -9; } if ((retcode=OCIBindByPos(stmthp, &bidhp[1],oractx->errhp,(ub4) 2, (dvoid *)exptype,(sb4)(strlen(exptype)+1),SQLT_STR,NULL, NULL, NULL, 0, NULL, (ub4)OCI_DEFAULT))!=OCI_SUCCESS) { ora_check_error(oractx->errhp,retcode, &errcode); LogMessage("ora_procedure_execute Error when OCIBindByPosexptype/n"); return -9; } if ((retcode=OCIBindByPos(stmthp, &bidhp[2],oractx->errhp,(ub4) 3, (dvoid*)conditionexp,(sb4)(strlen(conditionexp)+1),SQLT_STR, NULL, NULL, NULL, 0,NULL, (ub4)OCI_DEFAULT))!=OCI_SUCCESS) { ora_check_error(oractx->errhp,retcode, &errcode); LogMessage("ora_procedure_execute Error when OCIBindByPosconditionexp/n"); return -9; } //定義3個變量存儲輸出參數 char szResult[4096]; memset(szResult,0,sizeof(szResult)); int nErrorcode = 0 ; char szErrorMsg[4096]; memset(szErrorMsg,0,sizeof(szErrorMsg)); if ((retcode= OCIBindByPos(stmthp, &bidhp[3], oractx->errhp,(ub4)4,(dvoid*)(szResult),(sword)4096, SQLT_STR, &sb2aInd[0], NULL, NULL, 0, NULL, (ub4)OCI_DEFAULT))!=OCI_SUCCESS) { ora_check_error(oractx->errhp,retcode, &errcode); LogMessage("ora_procedure_execute Error when OCIBindByNamebusiness_date /n"); return -9; } if ((retcode=OCIBindByPos(stmthp, &bidhp[4], oractx->errhp,(ub4)5,(&nErrorcode),(sizeof(int)),SQLT_INT,&sb2aInd[1], NULL, NULL, 0, NULL,(ub4)OCI_DEFAULT))!=OCI_SUCCESS) { ora_check_error(oractx->errhp,retcode, &errcode); LogMessage("ora_procedure_execute Error when OCIBindByNamebussiness_nodeid /n"); return -9; } if ((retcode=OCIBindByPos(stmthp, &bidhp[5], oractx->errhp,(ub4)6, (dvoid*)(szErrorMsg), (sword)4096,SQLT_STR, &sb2aInd[2], NULL, NULL, 0, NULL,(ub4)OCI_DEFAULT))!=OCI_SUCCESS) { ora_check_error(oractx->errhp,retcode, &errcode); LogMessage("ora_procedure_execute Error when OCIBindByNamebusiness_date /n"); return -9; } retcode= OCIStmtExecute(oractx->svchp, stmthp, oractx->errhp, (ub4) 1,(ub4)0, (OCISnapshot *)NULL,(OCISnapshot*)NULL,(ub4)OCI_DEFAULT); ora_check_error(oractx->errhp, retcode, &errcode); if (0!=retcode) { LogMessage("OCIStmtExecute() failed.retcode=%d,errcode=%d,sql:%s", retcode, errcode, pszGBKSQL); if (OCI_ERROR==retcode&& errcode!=1756) { LogMessage("OCIStmtExecute() failed. erroroccured,disconnect"); OCIHandleFree(stmthp,OCI_HTYPE_STMT); ora_clean(oractx); delete pszGBKSQL; return -5; } } *errorcode= nErrorcode; *errormsg= newchar[sizeof(szErrorMsg) + 1]; memcpy(*errormsg,(char*)szErrorMsg,strlen(szErrorMsg)+ 1); *resultof= newchar[sizeof(szResult) + 1]; memcpy(*resultof,(char*)szResult,strlen(szResult)+ 1); //commit if((retcode=OCITransCommit(oractx->svchp, oractx->errhp, (ub4) 0)) !=OCI_SUCCESS) { ora_check_error(oractx->errhp,retcode, &errcode); LogMessage("ora_dbbind_execute Error when OCIStmtCommit/n"); return -6; } delete pszGBKSQL; OCIHandleFree(stmthp,OCI_HTYPE_STMT); return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 揭东县| 平利县| 蓝田县| 江达县| 从化市| 临洮县| 凯里市| 海安县| 金川县| 新昌县| 汨罗市| 大丰市| 乌什县| 青铜峡市| 全椒县| 凤阳县| 彰化县| 建水县| 鄂州市| 仪征市| 自贡市| 石城县| 吴堡县| 靖州| 永康市| 全州县| 绵竹市| 安化县| 新泰市| 黄梅县| 义马市| 论坛| 富裕县| 祁连县| 樟树市| 湖南省| 鹤山市| 丁青县| 皮山县| 梁平县| 邵阳县|