Home » Developer & Programmer » Precompilers, OCI & OCCI » OCI programming (merged 3)
OCI programming (merged 3) [message #402487] Mon, 11 May 2009 01:24 Go to next message
amit_thombre
Messages: 6
Registered: May 2009
Junior Member
HI All,

I am trying to use OCIColl datatype and thus wanted to check if a simple program using OCI functions and data types gets compile or not. sO I wrote the follwoing function which I want to compile as a DLL.

extern "C" CLUSTERINGDLL_API OCIColl *fnClusteringDLL(OCIColl *dxm[])
{
OCIError *errhp;
OCINumber num_1, num_2, sum;
OCINumberAdd(errhp, &num_1, &num_2, &sum);
std::cout<<"Members in the array are "<<dxm[1]<<std::endl;
std::cout<<"2 Member in the array are "<<dxm[0]<<std::endl;
printf("Is this printed %f", dxm[0]);
return dxm[1];
}
But I am getting the following error:-
Error 6 error LNK2019: unresolved external symbol _OCINumberAdd referenced in function _fnClusteringDLL clusteringdll.obj
I have included the library folder containing oci.lib in the project directories. Can somebody please help me in resolving this error?

Regards
Amit
Re: OCI programming [message #402505 is a reply to message #402487] Mon, 11 May 2009 03:34 Go to previous messageGo to next message
vicenzo
Messages: 28
Registered: December 2007
Location: Paris
Junior Member
Hi,

you have to tell the linker in which import library to find the function by adding oci.lib to the linker options or using a pragma.

The posted code will never work ! What are you trying to do ?
Re: OCI programming [message #402541 is a reply to message #402505] Mon, 11 May 2009 07:29 Go to previous messageGo to next message
amit_thombre
Messages: 6
Registered: May 2009
Junior Member
Thanks, I was able to remove the erro. I am just finding out if any of the OCI functions wrok properly or not. In fact i treid the following code for priting the sting , but it does not print the string. Can you please help in resolving this? the vstings does not get memery allocated. It has got 0x000000 as it's address.


OCIEnv *envhp=(OCIEnv *)0;
OCIError *errhp=(OCIError *)0;
OCIString *vstring1 = (OCIString *)0;
OCIString *vstring2 = (OCIString *)0;
text c_string[21];
text *text_ptr;
sword status;

strcpy((char *)c_string, "hello world");
/* Assign a text string to an OCIString */
status = OCIStringAssignText(envhp, errhp, c_string,
(ub4)strlen((char *)c_string),&vstring1);
/* Memory for vstring1 is allocated as part of string assignment */

status = OCIStringAssignText(envhp, errhp, (text *)"hello again",
// (ub4)strlen("This is a longer string."),&vstring1);
/* vstring1 is automatically resized to store the longer string */

/* Get a pointer to the string part of vstring1 */
text_ptr = OCIStringPtr(envhp, vstring1);
/* text_ptr now points to "hello world" */
printf("Value of text %s\n", text_ptr);
Re: OCI programming [message #402542 is a reply to message #402541] Mon, 11 May 2009 07:33 Go to previous messageGo to next message
vicenzo
Messages: 28
Registered: December 2007
Location: Paris
Junior Member
your environment and error handles are NULL...

Without proper handles, you cannot use theses functions...
Re: OCI programming [message #402543 is a reply to message #402542] Mon, 11 May 2009 07:38 Go to previous messageGo to next message
amit_thombre
Messages: 6
Registered: May 2009
Junior Member
how do I allocate handles, do I need to connect to the database , wiil the below code work

----------------------------------------------------------



OCIEnvCreate((OCIEnv **) &envhp, OCI_OBJECT, (dvoid *)0,
(dvoid * (*)(dvoid *, size_t)) 0, (dvoid * (*)(dvoid *, dvoid *, size_t))0,
(void (*)(dvoid *, dvoid *)) 0, (size_t) 0, (dvoid **) 0 );

OCIHandleAlloc (envhp, (dvoid **)&errhp, OCI_HTYPE_ERROR, (size_t)0,
(dvoid **)0);

OCIHandleAlloc(envhp, (dvoid **)&svrhp, OCI_HTYPE_SERVER, (size_t)0,
(dvoid **)0);

status = OCIServerAttach(svrhp, errhp, (text *)database,
(sb4)strlen((char *)database), OCI_DEFAULT);

if (status != OCI_SUCCESS)
{
printf("OCIServerAttach failed \n");
}
else
printf("OCIServerAttach - Success \n");

OCIHandleAlloc(envhp, (dvoid **)&svchp, OCI_HTYPE_SVCCTX, (size_t)0,
(dvoid **)0);
OCIAttrSet(svchp, OCI_HTYPE_SVCCTX, (dvoid *)svrhp, (ub4)0, OCI_ATTR_SERVER,
errhp);

OCIHandleAlloc(envhp, (dvoid **)&sesnhp, OCI_HTYPE_SESSION, (size_t)0,
(dvoid **)0);

OCIAttrSet(sesnhp, OCI_HTYPE_SESSION, (dvoid *)username,
(ub4)strlen((char *)username), OCI_ATTR_USERNAME, errhp);

OCIAttrSet(sesnhp, OCI_HTYPE_SESSION, (dvoid*)password,
(ub4)strlen((char *)password), OCI_ATTR_PASSWORD, errhp);

printf("Connecting as %s/%s@%s\n",username,password,database);

status = OCISessionBegin(svchp, errhp, sesnhp, OCI_CRED_RDBMS, OCI_DEFAULT);
if (status != OCI_SUCCESS)
{
printf("Connection failed \n");
}
else
printf("Connection - Success \n");

OCIAttrSet(svchp, OCI_HTYPE_SVCCTX, sesnhp, (ub4)0, OCI_ATTR_SESSION, errhp);

---------------------------------------------------------
OCI programming [message #402740 is a reply to message #402487] Tue, 12 May 2009 04:59 Go to previous messageGo to next message
amit_thombre
Messages: 6
Registered: May 2009
Junior Member
I am new to OCI programming. I am trying to call a stored procedure with an out parameter of VARRAY datatype. I have written the following code for the same but it is giving a run time error of "PLS-00306: wrong number or types of arguments in call to 'T'".

I am usign the followign code whcile calling the stored procedure.
----------------------------------------------------------
text *give_raise = (text *) "BEGIN\
T(:yvarray);\
END;";
OCIBind *bnd1p = NULL; /* the first bind handle */
OCIBind *bnd2p = NULL; /* the second bind handle */
OCIBind *bnd3p = NULL; /* the third bind handle */

//static void checkerr();
//sb4 status;
//sword yvarray;
OCIColl *yvarray;
dvoid *tmp;
OCISession *usrhp = (OCISession *)NULL;

-----------------------------------------------------------
I have tried using OCIArray but it is alo failing. Can somebody help me on this.

Amit
OCI programming [message #403243 is a reply to message #402487] Thu, 14 May 2009 07:15 Go to previous messageGo to next message
amit_thombre
Messages: 6
Registered: May 2009
Junior Member
I am trying to selec the values of y and x from a table where y and x are numbers. the code is as follows, I am getting the values as 0, whereas there are actual numeric values stored in them. :-

---------------------------
#include <oci.h>
#pragma comment(lib, "oci.lib")

int callProc(void);
sb4 ociCheckError(OCIError *errhp,sword status);

OCIEnv *envhp = NULL;
OCIServer *srvhp = NULL;
OCIError *errhp = NULL;
OCISvcCtx *svchp = NULL;
OCISession *seshp = NULL;
OCIStmt *stmhp = NULL;

#define MAXROWS 128
#define MAXCOLS 2
#define ROWSPERFETCH 4
//char *sql = "SELECT ename,job FROM emp WHERE deptno = :DEPT";
char *sql = "SELECT y,x FROM INPUT";
typedef struct {
float y;
float x;
} CO;

CO emp[MAXROWS];
float *colptr[MAXCOLS] = {&emp[0].y,&emp[0].x};
ub4 collen[MAXCOLS] = {sizeof(emp[0].y),sizeof(emp[0].x)};
ub2 coltyp[MAXCOLS] = {SQLT_STR,SQLT_STR};

// -------------------------------------------------------------------------
int
main(int argc, char *argv[])
{
char *sid="OEMREP";
char *usr="scott";
char *pwd="tiger";

// Initialise OCI
OCIInitialize(OCI_DEFAULT,NULL,NULL,NULL,NULL);

// Initialise OCI environment
OCIEnvInit(&envhp,OCI_DEFAULT,0,NULL);

// Allocate OCI error handler
if ( OCIHandleAlloc(envhp,(dvoid **)&errhp,OCI_HTYPE_ERROR,0,NULL)<0 ) {
return -1;
}

// Allocate OCI server
if ( ociCheckError(errhp,OCIHandleAlloc(envhp,(dvoid **)&srvhp,OCI_HTYPE_SERVER,0,NULL))<0 ) {
return -1;
}

// Allocate OCI service
if ( ociCheckError(errhp,OCIHandleAlloc(envhp,(dvoid **)&svchp,OCI_HTYPE_SVCCTX,0,NULL))<0 ) {
return -1;
}

// Attach to the server specified by sid
if ( ociCheckError(errhp,OCIServerAttach(srvhp,errhp,(text *)sid,strlen(sid),0))<0 ) {
return -1;
}
// Set the server attribute for the service
if ( ociCheckError(errhp,OCIAttrSet(svchp,OCI_HTYPE_SVCCTX,srvhp,0,OCI_ATTR_SERVER,errhp))<0 ) {
return -1;
}

// Allocate OCI Session
if ( ociCheckError(errhp,OCIHandleAlloc(envhp,(dvoid **)&seshp,OCI_HTYPE_SESSION,0,NULL))<0 ) {
return -1;
}
// Set the username attibute for the session
if ( ociCheckError(errhp,OCIAttrSet(seshp,OCI_HTYPE_SESSION,(text*)usr,strlen(usr),OCI_ATTR_USERNAME,errhp))<0 ) {
return -1;
}
// Set the password attibute for the session
if ( ociCheckError(errhp,OCIAttrSet(seshp,OCI_HTYPE_SESSION,(text*)pwd,strlen(pwd),OCI_ATTR_PASSWORD,errhp))<0 ) {
return -1;
}

// Start the session
ociCheckError(errhp,OCISessionBegin(svchp,errhp,seshp,OCI_CRED_RDBMS,OCI_DEFAULT));
// Set session attribute for the service
if ( ociCheckError(errhp,OCIAttrSet(svchp,OCI_HTYPE_SVCCTX,seshp,0,OCI_ATTR_SESSION,errhp))<0 ) {
return -1;
}

callProc();

if (seshp) {
// Stop the session
ociCheckError(errhp,OCISessionEnd(svchp,errhp,seshp,OCI_DEFAULT));
// Free the OCI Session
ociCheckError(errhp,OCIHandleFree(seshp,OCI_HTYPE_SESSION));
}

if (srvhp) {
// Detatch from the server
ociCheckError(errhp,OCIServerDetach(srvhp,errhp,OCI_DEFAULT));
// Free OCI server
ociCheckError(errhp,OCIHandleFree(srvhp,OCI_HTYPE_SERVER));
}

if (svchp) {
// Free OCI service
ociCheckError(errhp,OCIHandleFree(svchp,OCI_HTYPE_SVCCTX));
}

if (errhp) {
// Free OCI error handler
OCIHandleFree(errhp,OCI_HTYPE_ERROR);
}

return 0;
}

#define EXEC 1
#define FETCH 2

// -------------------------------------------------------------------------
int
callProc()
{
OCIBind *bindhp = NULL;
OCIDefine *defhp = NULL;
long deptno;
int r,c;
sb4 rowsProcessed,rowsFetched,rows;
int status;
int mode;

if ( ociCheckError(errhp,OCIHandleAlloc(envhp,(dvoid **)&stmhp,OCI_HTYPE_STMT,0,NULL))<0 )
return -1;

if ( ociCheckError(errhp,OCIStmtPrepare(stmhp,errhp,(text *)sql,strlen(sql),OCI_NTV_SYNTAX,OCI_DEFAULT))<0 )
return -1;

for ( c=0 ; c<MAXCOLS ; c++ ) {
if ( ociCheckError(errhp,OCIDefineByPos(stmhp,&defhp,errhp,c+1,colptr[c],collen[c],coltyp[c],NULL,NULL,NULL,OCI_DEFAULT))<0 )
return -1;
if ( ociCheckError(errhp,OCIDefineArrayOfStruct(defhp,errhp,sizeof(emp[0]),0,0,0))<0 )
return -1;
}

//deptno = 20;
//if ( ociCheckError(errhp,OCIBindByPos(stmhp,&bindhp,errhp,1,&deptno,sizeof(deptno),SQLT_INT,NULL,NULL,NULL,0,NULL,OCI_DEFAULT))<0 )
// return -1;

mode = EXEC;
rowsProcessed = 0;
do {
if ( mode==EXEC ) {
if ( (status=ociCheckError(errhp,OCIStmtExecute(svchp,stmhp,errhp,ROWSPERFETCH,0,NULL,NULL,OCI_DEFAULT)))<0 )
return -1;
mode = FETCH;
}
else {
if ( (status=ociCheckError(errhp,OCIStmtFetch(stmhp,errhp,ROWSPERFETCH,OCI_FETCH_NEXT,OCI_DEFAULT)))<0 )
return -1;
}
if ( ociCheckError(errhp,OCIAttrGet(stmhp,OCI_HTYPE_STMT,&rowsFetched,0,OCI_ATTR_ROW_COUNT,errhp))<0 )
return -1;

rows = rowsFetched - rowsProcessed;
for ( r=0 ; r<rows ; r++ )
printf("[%f][%f]\n",emp[r].y,emp[r].x);
rowsProcessed = rowsFetched;
} while ( status!=OCI_NO_DATA && status>=0 );
getch();
ociCheckError(errhp,OCIHandleFree(stmhp,OCI_HTYPE_STMT));
}

// -----------------------------------------------------------------------
sb4
ociCheckError(OCIError *errhp,sword status)
{
text errbuf[OCI_ERROR_MAXMSG_SIZE+1];
sb4 e;

if ( errhp==NULL ) {
printf("Error - Can't create error handler");
return -1;
}

switch ( status ) {
case OCI_SUCCESS:
break;
case OCI_SUCCESS_WITH_INFO:
OCIErrorGet(errhp,1,NULL,&e,errbuf,sizeof(errbuf),OCI_HTYPE_ERROR);
printf("SUCCESS WITH INFO - %s",errbuf);
break;
case OCI_NEED_DATA:
printf("Error - OCI_NEED_DATA");
break;
case OCI_NO_DATA:
//printf("Error - OCI_NODATA");
break;
case OCI_ERROR:
OCIErrorGet(errhp,1,NULL,&e,errbuf,sizeof(errbuf),OCI_HTYPE_ERROR);
printf("Error - %s",errbuf);
break;
case OCI_INVALID_HANDLE:
printf("Error - OCI_INVALID_HANDLE");
break;
case OCI_STILL_EXECUTING:
printf("Error - OCI_STILL_EXECUTE");
break;
case OCI_CONTINUE:
printf("Error - OCI_CONTINUE");
break;
default:
printf("Unknown Oracle error: Code %d",status);
break;
}
return status;
}
Re: OCI programming [message #403390 is a reply to message #402740] Fri, 15 May 2009 05:22 Go to previous message
vicenzo
Messages: 28
Registered: December 2007
Location: Paris
Junior Member
normal there is only one bind to do ! (:yvarray)

amit_thombre wrote on Tue, 12 May 2009 11:59
I am new to OCI programming. I am trying to call a stored procedure with an out parameter of VARRAY datatype. I have written the following code for the same but it is giving a run time error of "PLS-00306: wrong number or types of arguments in call to 'T'".

I am usign the followign code whcile calling the stored procedure.
----------------------------------------------------------
text *give_raise = (text *) "BEGIN\
T(:yvarray);\
END;";
OCIBind *bnd1p = NULL; /* the first bind handle */
OCIBind *bnd2p = NULL; /* the second bind handle */
OCIBind *bnd3p = NULL; /* the third bind handle */

//static void checkerr();
//sb4 status;
//sword yvarray;
OCIColl *yvarray;
dvoid *tmp;
OCISession *usrhp = (OCISession *)NULL;

-----------------------------------------------------------
I have tried using OCIArray but it is alo failing. Can somebody help me on this.

Amit

Previous Topic: OCI and bulk insert to stored procedure
Next Topic: return string from dll vc++ to oracle
Goto Forum:
  


Current Time: Thu Mar 28 08:51:14 CDT 2024