user-0 提问: AIX64 下oracle OCI使用问题

回复:
user: user-0
content: 以下是在Aix上写了个简单的小程序重现了一下问题。  OCIAttrSet 执行失败。“无效的属性值”。  // BugTest.cpp : 定义控制台应用程序的入口点。  #include <cfloat> // #else //#include <float.h> //#endif #include <iostream> #include <string.h> #include <time.h> #include <vector> #include "../../../LibShared/OCI/oracle_aix/include/oci.h" #include "Toolkit/UGOpenMP.h" #include "Toolkit/UGLogFile.h" #include "Base/ogdcdefs.h"  //! \brief 环境句柄 OCIEnv* envhp;       //! \brief 错误句柄 OCIError* errhp; //! \brief 服务器句柄 OCIServer* srvhp; //! \brief 上下文句柄 OCISvcCtx* svchp; //! \brief	用户句柄 OCISession* usrhp;  //! \brief 是否连接 bool connected;  //! brief 连接池 属性 OCICPool *poolhp;  //! brief 连接池名字 OraText *poolName; // \brief 语句 OCIStmt* stmthp;  using namespace std;  using namespace UGC;   //本程序在Unicode环境下运行。  void ConnectOra(const UGchar* dblink, const UGchar* username, const UGchar* password, 				const UGint nMinConn, const UGint nMaxConn, const UGint nIncConn) { 	stmthp = NULL; 	sword status; 	//使用Unicode字符连接 	int mode = OCI_OBJECT | OCI_THREADED ; 	OCIEnvNlsCreate(&envhp, mode, (dvoid *)0,  		(dvoid*(*)(dvoid *ctxp, size_t size))0,  		(dvoid*(*)(dvoid *ctxp, dvoid *memptr, size_t newsize))0,  		(void (*)(dvoid *ctxp, dvoid *memptr))0, 		(size_t)0, (dvoid**)0, 		(ub2)OCI_UCS2ID, (ub2)OCI_UCS2ID);  	//初始化：errhp and srvhp 	OCIHandleAlloc((dvoid*)envhp, (dvoid**)&errhp, (ub4)OCI_HTYPE_ERROR, 		(size_t)0, (dvoid**)0);  	OCIHandleAlloc((dvoid*)envhp, (dvoid**)&srvhp, (ub4)OCI_HTYPE_SERVER, 		(size_t)0, (dvoid**)0); 	status = OCIServerAttach(srvhp, errhp, (text*)dblink,  		(sb4)UGstrlen(dblink)*sizeof(UGchar), (ub4)OCI_DEFAULT); 	std::cout<< UGstrlen(dblink)*sizeof(UGchar)<<endl; 	if (status != OCI_SUCCESS) 	{ 		OCIServerDetach(srvhp, errhp, (ub4)OCI_DEFAULT); 		//释放环境变量：svchp, srvhp, and errhp 		OCIHandleFree((dvoid*)srvhp, (ub4)OCI_HTYPE_SERVER); 		OCIHandleFree((dvoid*)errhp, (ub4)OCI_HTYPE_ERROR); 		OCIHandleFree((dvoid*)envhp, (ub4)OCI_HTYPE_ENV); 		std::cout<< "//OCIServerAttach failed "<<status <<endl; 		return ; 	}  	//初始化：svchp 	OCIHandleAlloc((dvoid*)envhp, (dvoid**)&svchp, (ub4)OCI_HTYPE_SVCCTX,  		(size_t)0, (dvoid**)0); 	status = OCIAttrSet((dvoid*)svchp, (ub4)OCI_HTYPE_SVCCTX, (dvoid*)srvhp, (ub4)0,  		(ub4)OCI_ATTR_SERVER, errhp);  	std::cout<<"85 OCIHandleAlloc OCIAttrSet "<< status <<endl; 	//初始化：usrhp 	OCIHandleAlloc((dvoid*)envhp, (dvoid**)&usrhp, (ub4)OCI_HTYPE_SESSION,  		(size_t)0, (dvoid**)0); 	status =OCIAttrSet((dvoid*)usrhp, (ub4)OCI_HTYPE_SESSION, (dvoid*)username,  		(ub4)UGstrlen(username)*sizeof(UGchar), (ub4)OCI_ATTR_USERNAME, errhp); 	std::cout<<"91 OCIHandleAlloc OCIAttrSet "<< status <<endl;  	status =OCIAttrSet((dvoid*)usrhp, (ub4)OCI_HTYPE_SESSION, (dvoid*)password,  		(ub4)UGstrlen(password)*sizeof(UGchar), (ub4)OCI_ATTR_PASSWORD, errhp);  	std::cout<<"96 OCIHandleAlloc OCIAttrSet "<< status <<endl; 	//开始会话：Session 	status = OCISessionBegin(svchp, errhp, usrhp, OCI_CRED_RDBMS,  		OCI_DEFAULT); 	if (status != OCI_SUCCESS) 	{ 		//结束会话：Session 		OCISessionEnd(svchp, errhp, usrhp, (ub4)OCI_DEFAULT); 		OCIServerDetach(srvhp, errhp, (ub4)OCI_DEFAULT);  		//释放环境变量：svchp, srvhp, and errhp 		OCIHandleFree((dvoid*)svchp, (ub4)OCI_HTYPE_SVCCTX); 		OCIHandleFree((dvoid*)srvhp, (ub4)OCI_HTYPE_SERVER); 		OCIHandleFree((dvoid*)errhp, (ub4)OCI_HTYPE_ERROR); 		OCIHandleFree((dvoid*)envhp, (ub4)OCI_HTYPE_ENV); 		std::cout<< "//Session failed "<<status <<endl; 		return ; 	} 	status = OCIAttrSet((dvoid*)svchp, (ub4)OCI_HTYPE_SVCCTX, (dvoid*)usrhp, (ub4)0,  		(ub4)OCI_ATTR_SESSION, errhp); 	connected = true;  	std::cout<<"118 OCIHandleAlloc OCIAttrSet "<< status <<endl;  	std::cout<< status <<endl; } void testOracleCI() {  	//UGString strSQL = _U("insert into TestOracle(smdatasetid,Smx) values(6,1) returning SmDatasetID into :SmDTID"); 	UGchar* strSQL = L"select SmName from  TestOracle where SmName =:SmName";  	UGchar* strSever = L"ugotest64";//L"zhouqin"; 	UGchar* strUser  = L"datasetgrid_jA64";//L"tora1"; 	UGchar* strPasswd = L"datasetgrid_jA64";//L"1";  	ConnectOra(strSever,strUser,strPasswd,1,1,0); 	bool isConnected = connected; 	std::cout<< isConnected <<endl; 	if (isConnected) 	{ 		std::cout<< "Connect DataSource OK "<<endl;  		UGint nID = 1; 		UGchar* ucTempIn = L"Name1"; 		UGchar scName[64]; 		UGString str(ucTempIn); 		UGERROR(str); 		UGint nResult = 0; 		UGshort indID = 0; 		UGshort indRe = 0; 		//cursor.Open(); 		//openCusor 		OCIHandleAlloc((dvoid*)envhp, (dvoid**)&stmthp,  			(ub4)OCI_HTYPE_STMT, (size_t)0, (dvoid**)0);  		//cursor.Prepare(strSQL); 		OCIStmtPrepare(stmthp, errhp, (text*)strSQL,  			(ub4)UGstrlen(strSQL)*sizeof(UGchar), (ub4)OCI_NTV_SYNTAX,  			(ub4)OCI_DEFAULT);  		//cursor.BindByPos(1,&nID,sizeof(UGint),&indID,SQLT_INT); 		OCIBind *bindp = NULL;	 		sword status = OCIBindByPos(stmthp, &bindp, errhp, 1, (dvoid *)&nID,  			(sb4)sizeof(UGint), SQLT_INT, (dvoid *)&indID, (ub2 *)0,  			(ub2 *)0, (ub4)0, (ub4 *)0, (ub4)OCI_DEFAULT);  		std::cout<<"OCIBindByPos "<< status <<endl; 		ub2 formid = SQLCS_NCHAR;//SQLCS_IMPLICIT; 		status = OCIAttrSet ((void *) bindp, (ub4) OCI_HTYPE_BIND, (void *) &formid, 			(ub4) 0, (ub4)OCI_ATTR_CHARSET_FORM, errhp); //此处会返回 -1 错误为： 无效的属性值；  		std::cout<<"OCIAttrSet "<< status <<endl;  		sb4 errcode = 0; 		UGchar message[256]; 		if (status == OCI_ERROR) 		{ 			OCIErrorGet((dvoid*)errhp, (ub4)1, (text*)NULL, &errcode,  				(text*)message, (ub4)256, OCI_HTYPE_ERROR);  			UGString strError(message); 			UGERROR(strError); 		} 		 		//cursor.DefineByPos(1, &nDtID, sizeof(UGint), (void *)&indDtID, SQLT_INT); 		std::cout<<"sizeof(scName)  =  "<< sizeof(scName) <<endl; 		OCIDefine * defnp = NULL;  		status = OCIDefineByPos(stmthp, &defnp, errhp, (ub4)1,  			(dvoid *)&scName, (sb4)sizeof(scName), SQLT_STR,  			(dvoid *)&indRe, (ub2 *)0, (ub2 *)0,  			(ub4)OCI_DEFAULT); 		std::cout<<"OCIDefineByPos "<< status <<endl;   		status = OCIAttrSet ((void *) defnp, (ub4) OCI_HTYPE_DEFINE, (void *) &formid, 			(ub4) 0, (ub4)OCI_ATTR_CHARSET_FORM, errhp); //此处会返回 -1 错误为： 无效的属性值；   		std::cout<<"OCIDefineByPos OCIAttrSet "<< status <<endl;  		sb4 errcode1 = 0; 		UGchar message1[256]; 		if (status == OCI_ERROR) 		{ 			OCIErrorGet((dvoid*)errhp, (ub4)1, (text*)NULL, &errcode1,  				(text*)message1, (ub4)256, OCI_HTYPE_ERROR);  			UGString strError1(message1); 			UGERROR(strError1); 		}  		//cursor.Execute(1,__UGFILE__,__LINE__); 		status = OCIStmtExecute(svchp, stmthp, errhp,  			(ub4)1, (ub4)0, (OCISnapshot *)NULL,  			(OCISnapshot *)NULL, (ub4)OCI_DEFAULT); 		 		std::cout<<"OCIStmtExecute "<< status <<endl; 		if (status != OCI_SUCCESS) 		{ 			OCIErrorGet((dvoid*)errhp, (ub4)1, (text*)NULL, &errcode,  				(text*)message, (ub4)256, OCI_HTYPE_ERROR);  			UGString strError(message); 			UGERROR(strError); 		}  		UGString strReName(scName); 		UGERROR(strReName); 		std::cout<<"smdatasetid = "<<nResult <<endl;  	} 	std::cout<< "adfadf"<<endl; } int main() {	 	testOracleCI(); 	return 0; }