OCI 读取信息错误,懂的进来
为什么读取不了EMP数据表里的信息呢。。求解答
但是换在那个8I之前的OCI就能读取出来就是函数前面都带O的那个版本。
最现在这个版本怎么解决!!
int main(){ OCIEnv *myenvhp;//环境句柄 OCIServer *mysevhp;//服务句柄 OCIError *myerrorhp;//错误句柄 OCISession *myuserhp;//用户句柄 OCISvcCtx *mysvchp;//服务上下文句柄 OCIStmt *mystmt;//语句句柄 ub4 ub4recoedno=1; sb4 erroecode; char errormsg[1024]; //定义输出变量 OCIDefine *dbhp1=NULL; OCIDefine *dbhp2=NULL; int empno=0; int len1; char empname[50]={'\0'}; int len2; ub2 stmt_type; //定义sql语句 char sql[255]={'\0'}; sprintf(sql,"%s","select empno,ename from emp"); //创建环境,初始化oci环境 OCIEnvCreate(&myenvhp,OCI_THREADED|OCI_OBJECT,(dvoid*)0,0,0,0,(size_t)0,(dvoid**)0); //为服务句柄分配空间 OCIHandleAlloc((dvoid*)myenvhp,(dvoid**)&mysevhp,OCI_HTYPE_SERVER,0,(dvoid**)0); //为错误句柄分配空间 OCIHandleAlloc((dvoid*)myenvhp,(dvoid**)&myerrorhp,OCI_HTYPE_ERROR,0,(dvoid**)0); //追加数据库名到服务句柄 if (OCIServerAttach(mysevhp,myerrorhp,(text*)"orcl",strlen("orcl"),OCI_DEFAULT)==OCI_SUCCESS) printf("成功\n"); else { printf("失败\n"); if (OCIErrorGet(myerrorhp,ub4recoedno++,NULL,&erroecode,(oratext*)errormsg,sizeof(errormsg),OCI_HTYPE_ERROR)==OCI_SUCCESS) { printf("errorcode:%d/nerrormsg:%s",erroecode,errormsg); getch(); } return -1; } //为上下文句柄分配空间 OCIHandleAlloc((dvoid*)myenvhp,(dvoid**)&mysvchp,OCI_HTYPE_SVCCTX,0,(dvoid**)0); //设置上下文句柄值 OCIAttrSet((dvoid*)mysvchp,OCI_HTYPE_SVCCTX,(dvoid*)mysevhp,(ub4)0,OCI_ATTR_SERVER,myerrorhp); //为用户句柄分配空间 OCIHandleAlloc((dvoid*)myenvhp,(dvoid**)&myuserhp,OCI_HTYPE_SESSION,0,(dvoid**)0); //为用户句柄OCI_ATTR_USERNAME 附加值 OCIAttrSet((dvoid*)myuserhp,OCI_HTYPE_SESSION,(dvoid*)"scott",(ub4)strlen("scott"),OCI_ATTR_USERNAME,myerrorhp); //为用户句柄OCI_ATTR_PASSWORD 附加值 OCIAttrSet((dvoid*)myuserhp,OCI_HTYPE_SESSION,(dvoid*)"admin",(ub4)strlen("admin"),OCI_ATTR_PASSWORD,myerrorhp); //连接数据库打开一个会话 if (OCISessionBegin(mysvchp,myerrorhp,myuserhp,OCI_CRED_RDBMS,OCI_DEFAULT)==OCI_SUCCESS) printf("OK\n"); else { printf("fail\n"); return -1; } //为上下文句柄设置OCI_ATTR_SESSION 值 OCIAttrSet((dvoid*)mysvchp,OCI_HTYPE_SVCCTX,(dvoid*)myuserhp,(ub4)0,OCI_ATTR_SESSION,OCI_DEFAULT); //为SQL语句分配空间 if(OCIHandleAlloc((dvoid*)myenvhp,(dvoid**)&mystmt,OCI_HTYPE_STMT,(size_t)0,(dvoid**)0)!=OCI_SUCCESS) { printf("分配语句句柄错误"); getch(); return -1; } //绑定前的准备 if(OCIStmtPrepare(mystmt,myerrorhp,(text*)sql,(ub4)strlen((char*)sql),(ub4)OCI_NTV_SYNTAX,(ub4)OCI_DEFAULT)!=OCI_SUCCESS) { printf("绑定前准备错误"); getch(); return -1; } memset(empname,'\0',sizeof(empname)); //绑定第一列 len1=0; OCIDefineByPos(mystmt,&dbhp1,myerrorhp,1,(dvoid*)&empno,sizeof(int),SQLT_INT,NULL,&len1,NULL,OCI_DEFAULT); //绑定第二列 len2=0; OCIDefineByPos(mystmt,&dbhp2,myerrorhp,2,(dvoid*)&empname,sizeof(empname),SQLT_CHR,NULL,&len2,NULL,OCI_DEFAULT); //获得SQL类型 OCIAttrGet((dvoid*)mystmt,(ub4)OCI_HTYPE_STMT,(dvoid*)&stmt_type,(ub4*)0,(ub4)OCI_ATTR_STMT_TYPE,myerrorhp); //执行SQL OCIStmtExecute(mysvchp,mystmt,myerrorhp,(ub4)(stmt_type==OCI_STMT_SELECT?1:0),(ub4)0,(OCISnapshot *)NULL,(OCISnapshot *)NULL,OCI_DEFAULT); do { printf("empno:%d\tename:%s\n",empno,empname); }while(OCIStmtFetch2(mystmt,myerrorhp,1,OCI_FETCH_NEXT,1,OCI_DEFAULT)!=OCI_NO_DATA); //结束会话 OCISessionEnd(mysvchp,myerrorhp,myuserhp,OCI_DEFAULT); //断开数据库 OCIServerDetach(mysevhp,myerrorhp,OCI_DEFAULT); //释放所有句柄,当释放环境父句柄,它的子句柄内容都会自动释放 OCIHandleFree((void*)myenvhp,OCI_HTYPE_ENV); getch(); return 0;}