Reputation: 5681
I make some calls from C through OCI to an Oracle database.
I get this error:
OCI_INVALID_HANDLE - Invalid service context, connection or statement handle
I inherited this code:
initOracle(..) { .. //return successful or 0 }
int executeGetQuery(char * query, char * queryResultData) {
char stmt[255];
OCIError *errhp;
OCIDefine *defnp = (OCIDefine *) 0;
OCIStmt *stmthp;
OCISvcCtx *svchp; //server context handle (almost like DBPROCESS)
sb2 is_null;
memset((char *)stmt, 0, 255);
//TODO replace with query
sprintf((char *)stmt, "SELECT min(ID) as MINID FROM BNS_SAA_CONFIRMATIONS where SentToWssStatus<>'T'");
/* prepare statement */
if( checkerr(errhp, OCIStmtPrepare(stmthp, errhp, (text *) stmt,
(ub4) strlen((char *) stmt),
(ub4) OCI_NTV_SYNTAX, (ub4) OCI_DEFAULT)) == OCI_ERROR)
return -1;
/* define output params */
if(checkerr(errhp, OCIDefineByPos(stmthp, &defnp, errhp, 1, (dvoid *) queryResultData,
(sword) strlen(queryResultData), SQLT_STR, (dvoid *) &is_null, (ub2 *)0,
(ub2 *)0, OCI_DEFAULT)) == OCI_ERROR)//maybe replace SQLT_STR w SQLT_CHR OMFWTD
return -1;
/* execute the statement */
status = OCIStmtExecute(svchp, stmthp, errhp, (ub4) 1, (ub4) 0, (CONST OCISnapshot *) NULL, (OCISnapshot *) NULL, OCI_DEFAULT);
....
}
I think the error surfaces on these functions: OCIStmtExecute(..)
or OCIDefineByPos(..)
or OCIStmtPrepare(..)
Question: How do I get rid of this error and start executing the query?
Error documentation: http://docs.oracle.com/cd/B28359_01/appdev.111/b28395/oci02bas.htm
Upvotes: 0
Views: 7484
Reputation: 1264
Well... I see that OCI handles which are defined locally are not being either allocated and/or initialized correctly. If those steps are being done in some other code you inherited, you will need to ensure that you have a way to get the pointers to those handles. If not, the Oracle docs which you link to the error handling section of have both a great intro to OCI programming, and in-depth demo programs (you may have to create an Oracle developer account and download the OCI client to get them if they're not somewhere accessible to you). Good luck, the OCI is a huge API, but Oracle's own documentation is great once you learn how to navigate through it, other than the intro I found the OCI Relational functions section (at least it's equivalent in 10g) to be invaluable.
Upvotes: 1