本人原来是在VC中通过ado来操作Oracle数据库,没有用过oci来操作过oracle数据库,有哪位朋友,这样做过的可否给一此启发,或提供一个操作的例子。谢谢,急用。

解决方案 »

  1.   

    呵呵,帮你顶顶,我在学OCCI,你看看ORACLE的DOCUMENTATION吧,以后说不顶有问题向你请教.
    http://download-west.oracle.com/docs/cd/B10501_01/appdev.920/a96584/oci02bas.htm#419643只有英文的了,我在看OCCI的,有什么事再联系
      

  2.   

    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    #include <oratypes.h>
    #include <ocidfn.h>
    #ifdef _STDC_
    #include <ociapr.h>
    #else
    #include <ocikpr.h>
    #endif
    #include <ocidem.h>
    #define DEFER_PARSE 1
    #define VERSION_7  2
    Lda_Def lda;
    Cda_Def cda1;
    Cda_Def cda2;
    ub4 hda[HDA_SIZE/sizeof(ub4)];
    text *username=(text *)"SCOTT";
    text *password=(text *)"tiger";
    text *selectemp=(text*)"select ename,job from emp";
    text *insertemp=(text *)"insert into  SCOTT.EMP(EMPNO,ENAME,JOB,SAL,DEPTNO) values(:empno,:ename,:job,:sal,:deptno)";                 
    void insert(void);void select(void);
    text *ename,*job;
    sword empno,sal,deptno;void myflush()
    {
    eb1 buf[50];
    fgets((char*)buf,50,stdin);
    }/*define function insert*/
    void insert(void)
    {
    sword len,len2,flag,dsize,dsize2;

    sb2  db_type,db_type2;
    sb1  name_buf[20],name_buf2[20];
    sb4 enamelen,joblen;
    ub1 ch;
            flag=1;

          len=sizeof(name_buf);
      len2=len;
           if(odescr(&cda2,1,&enamelen,(sb2 *)&db_type,name_buf,(sb4*)&len,(sb4*)&dsize,(sb2 *)0,(sb2 *)0,(sb2 *)0)||
    odescr(&cda2,2,&joblen,(sb2 *)&db_type2,name_buf2,(sb4 *)&len2,(sb4*)&dsize2,(sb2 *)0,(sb2 *)0,(sb2 *)0))
    {
    printf("error to describe the dabase\n");
    return;
    }
         ename=(text *)malloc(enamelen+2);
     job=(text *)malloc(joblen+2);     // printf("success to describe the dabase\n");
        
    while(flag)
    {
    printf("------------------insert---------------------------------------------\n");
    if(obndrv(&cda1,(text *)":empno",-1,(ub1 *)&empno,(sword)sizeof(sword),INT_TYPE,-1,(sb2*)0,(text*)0,-1,-1)||
    obndrv(&cda1,(text *)":ename",-1,(ub1 *)ename,-1,STRING_TYPE,enamelen+1,(sb2*)0,(text*)0,-1,-1)||
    obndrv(&cda1,(text *)":job",-1,(ub1 *)job,-1,STRING_TYPE,joblen+2,(sb2*)0,(text*)0,-1,-1)||
    obndrv(&cda1,(text *)":sal",-1,(ub1 *)&sal,(sword)sizeof(sword),INT_TYPE,-1,(sb2*)0,(text*)0,-1,-1)||
    obndrv(&cda1,(text *)":deptno",-1,(ub1 *)&deptno,(sword)sizeof(sword),INT_TYPE,-1,(sb2*)0,(text*)0,-1,-1))
    {
    printf("error to bind input var\n");
    return;
    }
    else
    {
            printf("success to bind the value\n");
    }
         printf("please input empno:\n");
    myflush();
    scanf("%d",&empno);
    printf("please input ename:\n");
    myflush();
    fgets((char *)ename,(int)enamelen+1,stdin);
    printf("please input job:\n");
    //myflush();
    fgets((char *)job,(int)joblen+1,stdin);
    printf("please input sal:\n");
    //myflush();
    scanf("%d",&sal);
    printf("please input deptno\n");
    myflush();
    scanf("%d",&deptno);
    myflush();
    printf("success to get the value\n");
       
         if(oexec(&cda1))
    {
         printf("insert error\n");
         printf("success to insert ,press Q to quit,any key to continue\n");
        scanf("%c",&ch);
        myflush();
        if (ch=='q')
        flag=0;
    }
          else
    {
         if(ocom(&lda))
    {
         printf("cann't to commit,insert fail\n");
         return;
    }
    printf("success to insert ,press Q to quit,any key to continue\n");
    scanf("%c",&ch);
    myflush();
    if (ch=='q')
    flag=0;
    } }
    myflush();
    }void select(void)
    {

    ub1 ename[10],job[10];
    sword i;
    i=0;
    if (odefin(&cda2,1,(ub1*)ename,(sword)sizeof(ename),(sword)STRING_TYPE,(sword)-1,(sb2*)0,(text*)0,-1,-1,(ub2*)0,(ub2*)0)||
    odefin(&cda2,2,(ub1*)job,(sword)sizeof(job),(sword)STRING_TYPE,(sword)-1,(sb2*)0,(text*)0,-1,-1,(ub2*)0,(ub2*)0))
    {
    printf("error to bind out var \n");
    return;
    }
    else
    {
            printf("success to bind out var \n");
    } if(oexec(&cda2))
    {
    printf("error to select \n");
    return;
    }
        printf("success to execute the select statement\n");
     if(ofetch(&cda2))
    {
        if (cda2.rc==NO_DATA_FOUND)
    {
    printf(" no data selected\n");
    return;
    }
        if(cda2.rc==NULL_VALUE_RETURNED)
    {
    printf("error\n");
            return;
    }
    }
            printf("----------ename-----------------job---------\n");
        while(1)
    {


    printf("|         %s",ename);
    printf("            %s",job);
    printf("\n--------------------------------------------\n");

    i++;
    if(ofetch(&cda2))
    {
        if (cda2.rc==NO_DATA_FOUND)
    {
    printf(" data list out\n");
    break;
    }
       /* if(cda2.rc==NULL_VALUE_RETURNED)
    {
    printf("error\n");
            break;
    }*/
    }
            
    }

         printf("total %d people select!\n",i);
     myflush();
     return;
    }
    int main(int argc, char* argv[])
    {
    sword flag,f;
    f=1; if(olog(&lda,(ub1 *)hda,username,-1,password,-1,(text *)0,-1,(ub4)OCI_LM_DEF))
    {
    printf("connect fail\n");
    return 0;
    }
    else
    {
    printf("success to oracle use %s connect\n",username);
    }
    if (oopen(&cda1,&lda,(text*)0,-1,-1,(text)0,-1))
    {
    printf("open cursor cda1 failn\n");
    return 0;
    }
    else 
    {
    printf("success to open cursor cda1\n");
    }
    if (oopen(&cda2,&lda,(text*)0,-1,-1,(text)0,-1))
    {
    printf("open cursor cda2 failn\n");
    return 0;
    }
    else 
    {
    printf("success to open cursor cda2\n");
    }
       ocof(&lda);    if (oparse(&cda1,insertemp,(sb4)-1,FALSE,(ub4)VERSION_7))
    {
    printf("error to use SQl--insert\n ");
    return 0;
    }
    else
    {
    printf("success to prepare the insert statement\n");
        
    }

    if (oparse(&cda2,selectemp,(sb4)-1,FALSE,(ub4)VERSION_7))
    {
    printf("error to use SQl--select\n ");
    return 0;
    }
    else
    {
    printf("success to prepare the select statement\n");
        
    } while(f)
    {

    printf("---------------praprare to operate the database-----------------------\n");
    printf("---------------hit key 'I'to insert something to database\n");
    printf("---------------hit key 'S'to select something from database\n");
    printf("---------------hit key 'Q'to quit this system\n");
    printf("---------------please select!\n");

    scanf("%c",&flag);
    myflush();
    switch (flag)
    {
    case 'i':
    insert();
    break;
    case 's':
    select();
    break;
    case 'q':
    f=0;
    break;
    default:
    printf("----------------error select,please reselect\n");
    break;
    }

    }
    return 0;
    }
    用C写的,可以运行。你可以用$ORACLE_HOME/oci/sampales下的make 批处理文件编译
      

  3.   

    我做的例子,仅供参考:
    void cre_idx( TCHAR* area )
    {
    Cda_Def cda;
    text PLSQL[64];

    strcpy((char *)PLSQL, "BEGIN unicom_pack.create_idx('");
    strcat((char *)PLSQL, area);
    strcat((char *)PLSQL, "'); END;"); // 打开游标
    if ( oopen(&cda, &lda, (text *) 0, -1, -1, (text *) 0, -1) )
        {
    err_report(&cda);
    return;
        } if (oparse(&cda, PLSQL, (sb4) -1, FALSE, (ub4) VERSION_7))
        {
    err_report(&cda);
    oclose(&cda);
    return;
        } if ( oexec(&cda) )
    err_report(&cda);
    oclose(&cda); return;
    }