linux下应用oci编程示例

本示例程序基于对oci库方法的简单的封装,若要实验本程序,要求有linux下的oci库libclntsh.a或libclntsh.so。

下面是对其简单的封装:


#include <stdio.h>
 
#include "oratypes.h"

#include "ocidfn.h"
#ifdef __STDC__
#include "ociapr.h"
#else
#include "ocikpr.h"
#endif

#include "ocidem.h"

 

#define OCI_EXIT_FAILURE 1
#define OCI_EXIT_SUCCESS 0
 

#define  DEFER_PARSE        1
#define  NATIVE             1
#define  VERSION_7          2
 

 

 void logon(Lda_Def *lda,ub1 *hda,text *user,text* passwd);

void logon1(Lda_Def *lda,ub1 *hda,text *user,text* passwd);
void logoff (Lda_Def *lda,Cda_Def  *cda_cur);
void openCurs(Cda_Def *cda_cur,Lda_Def *lda_cur);
void parseSql(Cda_Def *cda_cur,text *sqlStmt,Lda_Def *lda);
void defineIntOut(Cda_Def *cda,Lda_Def *lda,int *out,sword index);
void defineTextOut(Cda_Def *cda,Lda_Def *lda,text *out,sword index,sword size);
void err_report(Cda_Def  *cursor,Lda_Def *lda);
void do_exit(eword status);
void close_cursor(Cda_Def *cda_cur);
void execute_sql(Cda_Def *cda_cur,Lda_Def *lda);
void bind_sql(Cda_Def *cda,text *txt,ub1 *ub,int size,int SQL_TYPE,Lda_Def *lda);

 

///////////////////////////////

ocihelper.h实现部分ocihelper.cpp

 //////////////////////////////


#include <stdio.h>
#include <stdlib.h>
#include "ocihelper.h"

void logon(Lda_Def *lda,ub1 *hda,text *user,text* passwd)
{

  if (olog(lda, (ub1 *)hda, (text *)user, -1, (text *)passwd, -1,
           (text *)0, -1, (ub4)OCI_LM_DEF))
  {
    err_report((Cda_Def *)lda,lda);
    exit(OCI_EXIT_FAILURE);
  }

  printf("\n Connected to ORACLE as %s\n",user);

}

void logon1(Lda_Def *lda,ub1 *hda,text *user,text* passwd)
{
    if(orlon( lda,hda,(text *)user,-1, (text *)passwd,-1,0))
  {
    exit(OCI_EXIT_FAILURE);
  }
   printf("\n Connected to ORACLE as %s\n",user);

}

void openCurs(Cda_Def *cda_cur,Lda_Def *lda_cur)
{

  if (oopen(cda_cur, lda_cur, (text *) 0, -1, -1, (text *) 0, -1))        
  {
    err_report(cda_cur,lda_cur);
    do_exit(OCI_EXIT_FAILURE);
  }
}


void parseSql(Cda_Def *cda_cur,text *sqlStmt,Lda_Def *lda)
{
  if (oparse(cda_cur, sqlStmt, (sb4) -1, DEFER_PARSE,                 
               (ub4) VERSION_7))
  {
    err_report(cda_cur,lda);
    do_exit(OCI_EXIT_FAILURE);
  }

}


void logoff(Lda_Def *lda,Cda_Def  *cda_cur)
{

  if (oclose(cda_cur))                                         
  {
    fprintf(stderr, "Error closing cursor 1.\n");
    do_exit(OCI_EXIT_FAILURE);
  }

  if (ologof(lda))                                 
  {
    fprintf(stderr, "Error on disconnect.\n");
    do_exit(OCI_EXIT_FAILURE);
  }

}

void close_cursor(Cda_Def *cda_cur)
{

  if (oclose(cda_cur))                                         
  {
    fprintf(stderr, "Error closing cursor 1.\n");
    do_exit(OCI_EXIT_FAILURE);
  }
}


void execute_sql(Cda_Def *cda_cur,Lda_Def *lda)
{
 
  if (oexec(cda_cur))
   {
     err_report(cda_cur,lda);
     do_exit(OCI_EXIT_FAILURE);
   }
}


void do_exit(eword status)
{

  if (status == OCI_EXIT_FAILURE)
     printf("\n Exiting with FAILURE status %d\n", status);
  else
     printf("\n Exiting with SUCCESS status %d\n", status);

  exit(status);
}


void err_report(Cda_Def  *cursor,Lda_Def *lda)
{
    sword n;
    text msg[512];                     

    if (cursor->fc > 0)
      printf("\n-- ORACLE error when processing OCI function %s \n\n",
            oci_func_tab[cursor->fc]);
    else
      printf("\n-- ORACLE error\n");

    n = (sword)oerhms(lda, cursor->rc, msg, (sword) sizeof msg);
    fprintf(stderr, "%s\n", msg);

}


void defineIntOut(Cda_Def *cda,Lda_Def *lda,int *out,sword index)
{
   if (odefin(cda, index, (ub1 *) out, (sword) sizeof(uword),
              (sword) SQLT_INT,
              (sword) -1, (sb2 *) 0, (text *) 0, -1, -1,
              (ub2 *) 0, (ub2 *) 0))
   {
     err_report(cda,lda);
     do_exit(OCI_EXIT_FAILURE);
   }
}


void defineTextOut(Cda_Def *cda,Lda_Def *lda,text *out,sword index,sword size)
{
 
    if (odefin(cda, index, (ub1 *) out, size,    
             (sword) SQLT_STR,
             (sword) -1, (sb2 *) 0, (text *) 0, -1, -1,
             (ub2 *) 0, (ub2 *) 0))
   {
     err_report(cda,lda);
     do_exit(OCI_EXIT_FAILURE);
   }
  
}

void bind_sql(Cda_Def *cda,text *txt,ub1 *ub,int size,int SQL_TYPE,Lda_Def *lda)
{
 if (obndrv(cda,txt , -1,ub,size,SQL_TYPE, -1, (sb2 *) 0, (text *) 0, -1, -1))          
 {
            err_report(cda,lda);
     do_exit(OCI_EXIT_FAILURE);
 }
}

 

 

然后将其打包为库:在此以两种方法库的形式给出,使用方法一样:都指出库路径所在目录或放在默认目录下即可

打包为动态链接库:
g++ -o libocihelper.so  -shared -rdynamic -fPIC -libclntsh.a

打包为静态链接库:

g++ -c ocihelper.cpp

ar -rc libocihelper.aocihelper.o libclntsh.a

 

////////////////////////////////////////////////////////////////////

下面是使用示例,首先定义全局变量,并包含头文件ocihelper.h

 Lda_Def lda;                                     
 ub1 hda[HDA_SIZE/(sizeof(ub1))];                  
 Cda_Def cda;

////////////////////////////////////////////////////////////////////////

1. select序列的值

 sword cert_id;

 logon( &lda, (ub1*)hda,(text *)"orausr",(text *)"ora123");
 openCurs(&cda,&lda);
 parseSql(&cda,(text *)"select cert_sequence.nextval from dual",&lda);
 defineIntOut(&cda,&lda,&cert_id, (sword)1);
 execute_sql(&cda,&lda);
 if(ofetch(&cda) == 0)
 {
      printf("cert_id:%d\n",cert_id);
    
 }
 close_cursor(&cda);

2.insert表值

text *insertStmt=(text *)"insert into certlist values(:certid,:userinfo,:inputtime);

 char local_time[12];
 time_t t;//获取当前日期
 t=time(NULL);
 struct tm *currtime;
 currtime=(struct tm*)localtime(&t);
 memset(local_time,0,sizeof(local_time));
 sprintf((char*)local_time,"%04d-%02d-%02d",1900+currtime->tm_year,1+currtime->tm_mon,currtime->tm_mday);

logon(&lda,(ub1 *)hda,(text *)"orausr",(text *)"ora123");
 openCurs(&cda,& lda);
 parseSql(&cda,insertStmt,&lda);
 bind_sql(&cda,(text *)":certid",(ub1 *)&cert_id,sizeof(cert_id),SQLT_INT,&lda);
 bind_sql(&cda,(text *)":userinfo",(ub1 *)user_info,strlen(user_info)+1,SQLT_STR,&lda);
 bind_sql(&cda,(text *)":inputtime",(ub1 *)local_time,strlen(local_time)+1,SQLT_STR,&lda);
 execute_sql(&cda, &lda);
 close_cursor(&cda); 
 ologof(&lda);

3.update表值

   text *updateQueryStmt=(text*)"update certlist set inputtime=:time where id=:certid";
   logon(&lda,(ub1 *)hda,(text *)"orausr",(text *)"ora123");
   openCurs(&cda,&lda);
   parseSql(&cda,updateQueryStmt,&lda); 
   bind_sql(&cda,(text *)":certid",(ub1 *)&cert_id,sizeof(cert_id),SQLT_INT,&lda);
   bind_sql(&cda,(text *)":time",(ub1 *)local_time,sizeof(local_time),SQLT_STR,&lda);
   execute_sql(&cda, &lda);
   close_cursor(&cda);

4.获取满足SQL条件的记录条数

void getDatabaseCount(text *sqlStmt,sword *count)
{
 openCurs(&cda,&lda);
 parseSql(&cda,sqlStmt,&lda);
 execute_sql(&cda,&lda);
 int count=0;
 while(ofetch(&cda)==0)
 {
    count++;
    printf("count:%d\n",count);             
 }
 *count=count;
 if (cda.rc!= NO_DATA_FOUND)
     err_report(&cda,&lda);
    close_cursor(&cda);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值