#include "stdlib.h"
#include "ocidfn.h"
#include "ocidem.h"
#include "oratypes.h"
#include "ocidem.h"
#include "ociapr.h"
#include "ocikpr.h"
#include "oci.h"
#include <stdio.h>
#include <string.h>
char USER[31];
char PASS_WORD[31];
char SERVERNAME[31];
char* SqlStat = "select out_date,rownum from kt_card_sign_file t where rownum<10";
OCIEnv *envhp = NULL; //环境句柄
OCIError *errhp = NULL; //错误句柄
OCIServer *srvhp = NULL; //服务器句柄
OCISvcCtx *svchp = NULL; //服务环境句柄
sword status;
void error_proc(dvoid *errhp, sword status)
{
text errbuf[512];
sb4 errcode;
switch (status)
{
case OCI_SUCCESS:
//printf("OCI_SUCCESS/n");
break;
case OCI_SUCCESS_WITH_INFO:
printf("OCI error: OCI_SUCCESS_WITH_INFO/n");
break;
case OCI_NEED_DATA:
printf("OCI error: OCI_NEED_DATA/n");
break;
case OCI_NO_DATA:
printf("OCI error: OCI_NO_DATA/n");
break;
case OCI_ERROR:
(void)OCIErrorGet((dvoid *)errhp,(ub4)1,NULL,&errcode,
errbuf,(ub4)sizeof(errbuf),OCI_HTYPE_ERROR);
printf("错误号:%d/n错误信息:%s/n",errcode,errbuf);
break;
case OCI_INVALID_HANDLE:
printf("OCI error: OCI_INVALID_HANDLE/n");
break;
case OCI_STILL_EXECUTING:
printf("OCI error: OCI_STILL_EXECUTING/n");
break;
default:
break;
}
}
int envInit(){
strcpy(USER,"adm_zhifb");
strcpy(PASS_WORD,"ali88");
strcpy(SERVERNAME,"dev-apay");
//创建环境句柄
OCIEnvCreate(&envhp, OCI_DEFAULT, 0,0,0,0,0,0);
//申请错误句柄
OCIHandleAlloc((dvoid *)envhp, (dvoid **)&errhp, OCI_HTYPE_ERROR, 0, 0);
//申请服务器句柄
error_proc(errhp, OCIHandleAlloc((dvoid *)envhp, (dvoid **)&srvhp, OCI_HTYPE_SERVER, 0, 0));
//申请服务环境句柄
error_proc(errhp, OCIHandleAlloc((dvoid *)envhp, (dvoid **)&svchp, OCI_HTYPE_SVCCTX, 0, 0));
//设置服务环境的服务器属性
error_proc(errhp, OCIAttrSet((dvoid *)svchp, OCI_HTYPE_SVCCTX, (dvoid *)srvhp, (ub4)0, OCI_ATTR_SERVER,
(OCIError *)errhp));
//连接数据库,建立会话
status = OCILogon(envhp,errhp,&svchp,reinterpret_cast<unsigned char *>(USER),(ub4)strlen((char *)USER),
(text *)PASS_WORD,(ub4)strlen((char *)PASS_WORD),(text *)SERVERNAME,(ub4)strlen((char *)SERVERNAME));
if(status==OCI_SUCCESS)
return 1;
return 0;
}
void main ()
{
OCIStmt *stmthp = NULL; //语句句柄
OCIBind *bnd1p = (OCIBind *)0; //结合句柄
OCIDefine *defhp; //定义句柄
OCIParam *mypard;
ub2 datelen;
text *date;
if(envInit())
printf("成功连接dev-apay数据库,已为%s用户建立会话./n",USER);
else{
printf("-----ORA_search,ERROR in OCILogon-----/n");
error_proc(errhp,status);
}
//申请语句句柄
error_proc(errhp, OCIHandleAlloc((dvoid *)envhp, (dvoid **)&stmthp, OCI_HTYPE_STMT,0,0));
//准备SQL语句
error_proc(errhp,OCIStmtPrepare(stmthp,errhp,reinterpret_cast<unsigned char *>(SqlStat),
(ub4)strlen((char *)SqlStat),OCI_NTV_SYNTAX,OCI_DEFAULT));
//执行SQL语句
if ((status=OCIStmtExecute(svchp,stmthp,errhp,(ub4)0,
(ub4)0,(CONST OCISnapshot *)NULL, (OCISnapshot *)NULL,OCI_DEFAULT)) && status != OCI_SUCCESS_WITH_INFO)
{
error_proc(errhp,status);
printf("-----ORA_search,ERROR in OCIStmtExecute-----/n");
return;
}
//得到字段参数
error_proc(errhp, OCIParamGet(stmthp,OCI_HTYPE_STMT, errhp, (dvoid **)&mypard, (ub4) 1));
//得到字段属性(数据长度,数据类型等)
error_proc(errhp, OCIAttrGet((dvoid*) mypard, (ub4) OCI_DTYPE_PARAM,
(dvoid*) &datelen, (ub4 *) 0, (ub4) OCI_ATTR_DATA_SIZE,
(OCIError *) errhp ));
date = (text *) malloc((int) datelen + 1);
//设置输出变量
error_proc(errhp,OCIDefineByPos(stmthp, &defhp, errhp,1, (ub1 *) date, datelen+1,SQLT_CHR, (dvoid *) 0,
0,(ub2 *) 0, OCI_DEFAULT));
//遍历resultSet
while(OCIStmtFetch(stmthp,errhp,1, OCI_FETCH_NEXT, OCI_DEFAULT) !=OCI_NO_DATA ){
printf("The date is %s/n",date);
}
//断开连接
error_proc(errhp,OCILogoff(svchp,errhp));
return;
}