Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: Connect to an Oracle DB from a C program

Re: Connect to an Oracle DB from a C program

From: George Dau <gedau_at_isa.mim.com.au>
Date: Tue, 21 Sep 1999 02:52:17 GMT
Message-ID: <37e6f186.14505638@158.54.6.109>


"MV" <mv_at_yahoo.com> wrote:

]Hi:
]I have to connect to an Oracle Database from a C program... Could anyone
]help me finding a pattern program for such a connection.
]Would a shell script be more appropriate (I should not use any Perl
]actually).
]Thanks,
]Edouard
]

Sorry, this doesn't do anything except log in and log out, and probably does not even compile at the moment, but might give you start. What I wanted was ecOCI that did all the real dirty stuff, which I could call easily from a more simple program. Here is ecOCI:



#include <oracle/oci.h>

boolean ecOCIloggedon=FALSE;

sword	ecOCIstatus;
OCIEnv		*ecOCIenvhp;
OCISvcCtx	*ecOCIsvchp;
OCIError	*ecOCIerrhp;
OCIStmt		*ecOCIsqlhp;

void ecOCIlogoff () {

if (ecOCIloggedon) OCILogoff(ecOCIsvchp, ecOCIerrhp);
if (ecOCIenvhp) OCIHandleFree(ecOCIenvhp, OCI_HTYPE_ENV);
if (ecOCIerrhp) OCIHandleFree(ecOCIerrhp, OCI_HTYPE_ERROR);
if (ecOCIsqlhp) OCIHandleFree(ecOCIsqlhp, OCI_HTYPE_STMT);
ecOCIloggedon=FALSE;
}

void ecOCIchck(sword status) {
text errbuf[512];
sb4 errcode = 0;

switch (status) {
case OCI_SUCCESS:
  break;
case OCI_SUCCESS_WITH_INFO:
  OCIErrorGet((dvoid *)ecOCIerrhp, (ub4) 1, (text *) NULL, &errcode,

               errbuf, (ub4) sizeof(errbuf), OCI_HTYPE_ERROR);   printf("ecOCI Warn: %s",errcode, errbuf);   break;
default:
  OCIErrorGet((dvoid *)ecOCIerrhp, (ub4) 1, (text *) NULL, &errcode,

               errbuf, (ub4) sizeof(errbuf), OCI_HTYPE_ERROR);   printf("ecOCI Error: %s", errbuf);
  ecOCIlogoff();
  exit (-1); }
}

int ecOCIlogon (text *user, text *pswd ,text *sid) { if (OCIInitialize((ub4) OCI_DEFAULT, (dvoid *) 0, (dvoid * (*) ()) 0, (dvoid * (*) ()) 0, (void (*) ()) 0)) {   printf("ecOCI Error: OCIInitialize failed.\n");   exit (-1);
}
if (OCIEnvInit(&ecOCIenvhp, (ub4) OCI_DEFAULT, (size_t) 0, (dvoid **) 0)) {
  printf("ecOCI Error: OCIEnvInit failed.\n");   if (ecOCIenvhp) OCIHandleFree(ecOCIenvhp, OCI_HTYPE_ENV);   exit (-1);
}
if (OCIHandleAlloc(ecOCIenvhp,(dvoid **) &ecOCIerrhp, (ub4) OCI_HTYPE_ERROR, (size_t) 0, (dvoid **) 0)) {   printf("ecOCI Error: OCIHandleAlloc could not allocate error handle.\n");
  if (ecOCIenvhp) OCIHandleFree(ecOCIenvhp, OCI_HTYPE_ENV);   if (ecOCIerrhp) OCIHandleFree(ecOCIerrhp, OCI_HTYPE_ERROR);   exit (-1);
}
if (OCIHandleAlloc(ecOCIenvhp,(dvoid **) &ecOCIsqlhp, (ub4) OCI_HTYPE_STMT, (size_t) 0, (dvoid **) 0)) {   printf("ecOCI Error: OCIHandleAlloc could not allocate sql statement handle.\n");

  if (ecOCIenvhp) OCIHandleFree(ecOCIenvhp, OCI_HTYPE_ENV);
  if (ecOCIerrhp) OCIHandleFree(ecOCIerrhp, OCI_HTYPE_ERROR);
  if (ecOCIsqlhp) OCIHandleFree(ecOCIsqlhp, OCI_HTYPE_STMT);
  exit (-1);
}
ecOCIchck(OCILogon(ecOCIenvhp, ecOCIerrhp, &ecOCIsvchp, user, strlen(user),

          pswd, strlen(pswd), sid, strlen(sid))); ecOCIloggedon=TRUE;
return(0);}

/*

int main () {
#include "ecOCI.c"

/*

ecOCIstatus = 0;
while (ecOCIstatus == 0) {
  ecOCIstatus = OCIStmtFetch(ecOCIsqlhp, ecOCIerrhp, (ub4) 1, (ub4) OCI_FETCH_NEXT, (ub4) OCI_DEFAULT);
  if (ecOCIstatus != 100) {ecOCIchck(ecOCIstatus);}   printf("%d ", ecOCIstatus);
  printf("%s %s %s %d\n",owner,table_name,tablespace_name,num_rows); }

/*

--
 ,-,_|\ George Dau - Unix (Solaris, DEC Unix, Linux), Oracle, Internet. __

/    * \ Home: gedau_at_pobox.com         !  Any views or opinions expressed (OO)
\_,--\_/ Work: gedau_at_isa.mim.com.au    !  above may be mine, but are NOT ( \/ )
      v   WWW: http://pobox.com/~gedau !  necessarily those of M.I.M.     W--W
Received on Mon Sep 20 1999 - 21:52:17 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US