自己写的oci查询程序

来源:互联网 发布:苹果电脑如何赚钱知乎 编辑:程序博客网 时间:2024/05/14 12:19
自己写的oci查询程序2008-06-21 11:23:18

分类: Oracle

程序只是写了一个简单的例子,不完善,接口写得不行
 

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<oci.h>
typedef struct {
    OCIEnv         *EnvHP;
    OCIServer     *SrvHP;
  OCISvcCtx *SvcHP;
    OCIError     *ErrHP;
    OCISession     *SessionHP;
    OCIStmt *StmtHP;
} MY_OCI_CONTEXT_T;
int oci_init(MY_OCI_CONTEXT_T* ociCtx_p);
static int select_data(MY_OCI_CONTEXT_T* ociCtx_p,int pid);
void oci_clean(MY_OCI_CONTEXT_T* ociCtx_p);

int main()
{
  MY_OCI_CONTEXT_T ociCtx;
    oci_init(&ociCtx);
  
  select_data(&ociCtx,1110);
  
  oci_clean(&ociCtx);
  return 0;
}

int oci_init(MY_OCI_CONTEXT_T* ociCtx_p)
{
    sword sr;
    char tempstr[128];
    sr=OCIEnvInit((OCIEnv **)&ociCtx_p->EnvHP, OCI_DEFAULT,(size_t) 0,
                     (dvoid **) 0);
                     
  sr=OCIEnvCreate(&ociCtx_p->EnvHP,OCI_DEFAULT,0,0,0,0,0,0);
  if(sr!=OCI_SUCCESS)
  {
    return -1;
  }
  
  sr= OCIHandleAlloc((dvoid *) ociCtx_p->EnvHP,(dvoid **)&ociCtx_p->ErrHP, OCI_HTYPE_ERROR,
                   (size_t) 0,(dvoid **) 0);/* server contexts */
  if(sr!=OCI_SUCCESS)
  {
    return -1;
  }
  
  sr=OCIHandleAlloc((dvoid *)ociCtx_p->EnvHP,(dvoid **)&ociCtx_p->SrvHP, OCI_HTYPE_SERVER,
                   (size_t) 0,(dvoid **) 0);
  if(sr!=OCI_SUCCESS)
  {
    return -1;
  }
  sr=OCIHandleAlloc((dvoid *)ociCtx_p->EnvHP,(dvoid **)&ociCtx_p->SvcHP, OCI_HTYPE_SVCCTX,
                   (size_t) 0,(dvoid **) 0);
  if(sr!=OCI_SUCCESS)
  {
    return -1;

  }
  strcpy(tempstr,"ORCL");
  printf("servername = %s\n",tempstr);


  sr= OCIServerAttach(ociCtx_p->SrvHP,ociCtx_p->ErrHP,(text *)tempstr,
                   strlen(tempstr), 0);
  if(sr!=OCI_SUCCESS)
  {
    return -1;
  }

  /* set attribute server context in the service context */
  (void) OCIAttrSet((dvoid *)ociCtx_p->SvcHP, OCI_HTYPE_SVCCTX, (dvoid *)ociCtx_p->SrvHP,
                 (ub4) 0, OCI_ATTR_SERVER,(OCIError *) ociCtx_p->ErrHP);
                 
  sr= OCIHandleAlloc((dvoid*)ociCtx_p->EnvHP,(dvoid **)&ociCtx_p->SessionHP,
                 (ub4) OCI_HTYPE_SESSION,(size_t) 0,(dvoid **) 0);
  if(sr!=OCI_SUCCESS)
  {
    return -1;
  }
    strcpy(tempstr,"admin");
  (void) OCIAttrSet((dvoid*)ociCtx_p->SessionHP,(ub4) OCI_HTYPE_SESSION,
                 (dvoid *)tempstr,(ub4)strlen((char*)tempstr),
                 (ub4) OCI_ATTR_USERNAME, ociCtx_p->ErrHP);

     strcpy(tempstr,"admin");
     printf("password = %s\n",tempstr);

 (void) OCIAttrSet((dvoid*)ociCtx_p->SessionHP,(ub4) OCI_HTYPE_SESSION,
                 (dvoid *) tempstr,(ub4)strlen((char*)tempstr),
                 (ub4) OCI_ATTR_PASSWORD, ociCtx_p->ErrHP);

  sr=OCISessionBegin (ociCtx_p->SvcHP, ociCtx_p->ErrHP, ociCtx_p->SessionHP, OCI_CRED_RDBMS,
                  (ub4) OCI_DEFAULT);
  if(sr!=OCI_SUCCESS)
  {
          return -1;
  }

  (void) OCIAttrSet((dvoid*)ociCtx_p->SvcHP,(ub4) OCI_HTYPE_SVCCTX,
                   (dvoid *)ociCtx_p->SessionHP,(ub4) 0,
                   (ub4) OCI_ATTR_SESSION, ociCtx_p->ErrHP);


        sr=OCIHandleAlloc((dvoid *)ociCtx_p->EnvHP,(dvoid **)&ociCtx_p->StmtHP,
                           OCI_HTYPE_STMT,(size_t) 0,(dvoid **) 0);
  if(sr!=OCI_SUCCESS)
  {
    return -1;
  }
  return 1;
}
    
static int select_data(MY_OCI_CONTEXT_T* ociCtx_p,int pid)
{
    char sqlcmd[128]="";
    sword retcode;
    OCIDefine *defcolp[3];
    sword indp[3];
    int pointId;
    char pointName[30];
    char pointDesc[60];
    sprintf(sqlcmd,"select c_point_id,c_point_name,C_POINT_DESC from t_point where c_point_id=%d",pid);
    retcode = OCIStmtPrepare (ociCtx_p->StmtHP, ociCtx_p->ErrHP,(unsignedchar *)sqlcmd,
                              (ub4)strlen((char*)sqlcmd),
                              (ub4) OCI_NTV_SYNTAX,(ub4) OCI_DEFAULT
                              );
                              
    if(retcode!=OCI_SUCCESS)
    {
        printf("error OCIStmtPrepare \n");
    }

    retcode=OCIDefineByPos(ociCtx_p->StmtHP,&defcolp[0], ociCtx_p->ErrHP,(ub4)1,(dvoid *)&pointId,
                           (sb4)sizeof(int), SQLT_INT , (dvoid *)&indp[0],(ub2 *)0,
                           (ub2 *)0,(ub4) OCI_DEFAULT);
    if(retcode!=OCI_SUCCESS)
    {
        printf("error OCIDefineByPos 0 \n");
    }

    retcode=OCIDefineByPos(ociCtx_p->StmtHP,&defcolp[1], ociCtx_p->ErrHP,(ub4)2,(dvoid *)pointName,
                          (sb4)sizeof(pointName),SQLT_CHR, (dvoid*)&indp[1],(ub2 *)0,
                          (ub2 *)0,(ub4) OCI_DEFAULT);

    if(retcode!=OCI_SUCCESS)
    {
        printf("error OCIDefineByPos 1\n");
    }

    retcode=OCIDefineByPos(ociCtx_p->StmtHP,&defcolp[2], ociCtx_p->ErrHP,(ub4)3,(dvoid *)pointDesc,
                          (sb4)sizeof(pointDesc),SQLT_CHR, (dvoid*)&indp[2],(ub2 *)0,
                          (ub2 *)0,(ub4) OCI_DEFAULT);
    if(retcode!=OCI_SUCCESS)
    {
        printf("error OCIDefineByPos 2\n");
    }
    
    retcode=OCIStmtExecute(ociCtx_p->SvcHP, ociCtx_p->StmtHP, ociCtx_p->ErrHP,(ub4) 1,(ub4) 0,
                          (CONST OCISnapshot*)NULL,(OCISnapshot *) NULL, OCI_DEFAULT );
    if(retcode!=OCI_SUCCESS&&retcode!=OCI_NO_DATA)
    {
        printf("error OCIStmtExecute \n");
    }
    
    printf("tst---->pid %d ptName %s ptDesc %s\n",pointId,pointName,pointDesc);
    return 1;
}

void oci_clean(MY_OCI_CONTEXT_T* ociCtx_p)
{
    printf("\n ########## clean up ############ \n");
                 
    if (OCISessionEnd(ociCtx_p->SvcHP, ociCtx_p->ErrHP,
                      ociCtx_p->SessionHP,(ub4) 0))
         printf("FAILED: OCISessionEnd()\n");
    
  
    if (OCIServerDetach(ociCtx_p->SrvHP, ociCtx_p->ErrHP,(ub4) OCI_DEFAULT))
         printf("FAILED: OCIServerDetach()\n");
     
     printf("Detached from server.\n");
                                    
   printf("Freeing handles ...\n");
  if (ociCtx_p->StmtHP)
     OCIHandleFree((dvoid*) ociCtx_p->StmtHP,(ub4) OCI_HTYPE_STMT);
  if (ociCtx_p->ErrHP)
     OCIHandleFree((dvoid*) ociCtx_p->ErrHP,(ub4) OCI_HTYPE_ERROR);
  if (ociCtx_p->SrvHP)
     OCIHandleFree((dvoid*) ociCtx_p->SrvHP,(ub4) OCI_HTYPE_SERVER);
  if (ociCtx_p->SvcHP)
     OCIHandleFree((dvoid*) ociCtx_p->SvcHP,(ub4) OCI_HTYPE_SVCCTX);
  if (ociCtx_p->SessionHP)
     OCIHandleFree((dvoid*) ociCtx_p->SessionHP,(ub4) OCI_HTYPE_SESSION);
  if (ociCtx_p->EnvHP)
     OCIHandleFree((dvoid*) ociCtx_p->EnvHP,(ub4) OCI_HTYPE_ENV);
}

阅读(1908) | 评论(0) | 转发(0) |
0

上一篇:ntp服务器和客户端的配置文件

下一篇:研究证实男孩不坏女孩不爱



0 0
原创粉丝点击