0 Replies Latest reply: Dec 12, 2006 1:12 AM by 551272 RSS

    what's error in my program

    551272
      THIS IS MY CODE,It can pass compiled and run.but the resut is:
      when it executed the OCISessionBegin() and has a error and couldn't run.
      can you tell me.FAILED: OCISessionBegin(). Make sure database is up and the username/password is valid#include "stdio.h"

      extern"C"{
      #ifndef XML_ORACLE
      #include <xml.h>
      #endif
      }

      #include <stdlib.h>
      #include <string.h>

      extern"C"
      {
      #ifndef OCIXMLDB_ORACLE
      #include <ocixmldb.h>
      #endif
      }

      extern"C"
      {
      #ifndef ORATYPES_ORACLE
      #include <oratypes.h>
      #endif
      }
      OCIEnv *envhp;
      OCIError *errhp;
      OCISvcCtx *svchp;
      OCIStmt *stmthp;
      OCIServer *srvhp;
      OCIDuration dur;
      OCISession *sesshp;
      oratext username[] = "tempsys";
      oratext password[] = "ssssss";
      oratext filename[33] = "AMCEWEN-20021009123336171PDT.xml";
      oratext schemaloc[]= "http://localhost:8080/sys/schemas/PUBLIC/xmlns.oracle.com/xdb/XDBSchema.xsd";
      /* Execute a SQL statement that binds XML data */
      sword exec_bind_xml(OCISvcCtx svchp, OCIError errhp, OCIStmt *stmthp,
      void xml, OCIType xmltdo, OraText *sqlstmt)
      {
      OCIBind bndhp1 = (OCIBind ) 0;
      sword status = 0;
      OCIInd ind = OCI_IND_NOTNULL;
      OCIInd *indp = &ind;
      if(status = OCIStmtPrepare(stmthp, errhp, (OraText *)sqlstmt,
      (ub4)strlen((const char *)sqlstmt),
      (ub4) OCI_NTV_SYNTAX, (ub4) OCI_DEFAULT))
      return OCI_ERROR;
      if(status = OCIBindByPos(stmthp, &bndhp1, errhp, (ub4) 1, (dvoid *) 0,
      (sb4) 0, SQLT_NTY, (dvoid *) 0, (ub2 *)0,
      (ub2 *)0, (ub4) 0, (ub4 *) 0, (ub4) OCI_DEFAULT))
      return OCI_ERROR;
      if(status = OCIBindObject(bndhp1, errhp, (CONST OCIType *) xmltdo,
      (dvoid **) &xml, (ub4 *) 0,
      (dvoid **) &indp, (ub4 *) 0))
      return OCI_ERROR;
      if(status = OCIStmtExecute(svchp, stmthp, errhp, (ub4) 1, (ub4) 0,
      (CONST OCISnapshot*) 0, (OCISnapshot*) 0,
      (ub4) OCI_DEFAULT))
      return OCI_ERROR;
      return OCI_SUCCESS;
      }
      /* Initialize OCI handles, and connect */
      sword init_oci_connect()
      {
      sword status;
      if (OCIEnvCreate((OCIEnv **) &(envhp), (ub4) OCI_OBJECT,
      (dvoid *) 0, (dvoid * (*)(dvoid *,size_t)) 0,
      (dvoid * (*)(dvoid *, dvoid *, size_t)) 0,
      (void (*)(dvoid *, dvoid *)) 0, (size_t) 0, (dvoid **) 0))
      {
      printf("FAILED: OCIEnvCreate()\n");
      return OCI_ERROR;
      }
      /* Allocate error handle */
      if (OCIHandleAlloc((dvoid *) envhp, (dvoid **) &(errhp),
      (ub4) OCI_HTYPE_ERROR, (size_t) 0, (dvoid **) 0))
      {
      printf("FAILED: OCIHandleAlloc() on errhp\n");
      return OCI_ERROR;
      }
      /* Allocate server handle */
      if (status = OCIHandleAlloc((dvoid *) envhp, (dvoid **) &srvhp,
      (ub4) OCI_HTYPE_SERVER, (size_t) 0, (dvoid **) 0))
      {
      printf("FAILED: OCIHandleAlloc() on srvhp\n");
      return OCI_ERROR;
      }
      /* Allocate service context handle */
      if (status = OCIHandleAlloc((dvoid *) envhp,
      (dvoid **) &(svchp), (ub4) OCI_HTYPE_SVCCTX,
      (size_t) 0, (dvoid **) 0))
      {
      printf("FAILED: OCIHandleAlloc() on svchp\n");
      return OCI_ERROR;
      }
      /* Allocate session handle */
      if (status = OCIHandleAlloc((dvoid *) envhp, (dvoid **) &sesshp ,
      (ub4) OCI_HTYPE_SESSION, (size_t) 0, (dvoid **) 0))
      {
      printf("FAILED: OCIHandleAlloc() on sesshp\n");
      return OCI_ERROR;
      }
      /* Allocate statement handle */
      if (OCIHandleAlloc((dvoid *)envhp, (dvoid **) &stmthp,
      (ub4)OCI_HTYPE_STMT, (CONST size_t) 0, (dvoid **) 0))
      {
      printf("FAILED: OCIHandleAlloc() on stmthp\n");
      return status;
      }
      if (status = OCIServerAttach((OCIServer *) srvhp, (OCIError *) errhp,
      (CONST oratext *)"", 0, (ub4) OCI_DEFAULT))
      {
      printf("FAILED: OCIServerAttach() on srvhp\n");
      return OCI_ERROR;
      }
      /* Set server attribute to service context */
      if (status = OCIAttrSet((dvoid *) svchp, (ub4) OCI_HTYPE_SVCCTX,
      (dvoid *) srvhp, (ub4) 0, (ub4) OCI_ATTR_SERVER,
      (OCIError *) errhp))
      {
      printf("FAILED: OCIAttrSet() on svchp\n");
      return OCI_ERROR;
      }
      /* Set user attribute to session */
      if (status = OCIAttrSet((dvoid *)sesshp, (ub4) OCI_HTYPE_SESSION,
      (dvoid *)username,
      (ub4) strlen((const char *)username),
      (ub4) OCI_ATTR_USERNAME, (OCIError *) errhp))
      {
      printf("FAILED: OCIAttrSet() on authp for user\n");
      return OCI_ERROR;
      }
      /* Set password attribute to session */
      if (status = OCIAttrSet((dvoid *) sesshp, (ub4) OCI_HTYPE_SESSION,
      (dvoid *)password,
      (ub4) strlen((const char *)password),
      (ub4) OCI_ATTR_PASSWORD, (OCIError *) errhp))
      {
      printf("FAILED: OCIAttrSet() on authp for password\n");
      return OCI_ERROR;
      }
      /* Begin a session (OCISession *) sesshp, (ub4) OCI_CRED_RDBMS,*/
      if (status = OCISessionBegin((OCISvcCtx *) svchp,
      (OCIError *) errhp,
      (OCISession *) sesshp, (ub4) OCI_CRED_RDBMS,
      (ub4) OCI_STMT_CACHE)){
      printf("FAILED: OCISessionBegin(). Make sure database is up and the username/password is valid. \n");
      return OCI_ERROR;
      }
      /* Set session attribute to service context */
      if (status = OCIAttrSet((dvoid *) svchp, (ub4) OCI_HTYPE_SVCCTX,
      (dvoid *)sesshp, (ub4) 0, (ub4) OCI_ATTR_SESSION,
      (OCIError *) errhp))
      {
      printf("FAILED: OCIAttrSet() on svchp\n");
      return OCI_ERROR;
      }
      return OCI_SUCCESS;
      }
      /* Free OCI handles, and disconnect */
      void free_oci()
      {
      sword status = 0;
      /* End the session */
      if (status = OCISessionEnd((OCISvcCtx *)svchp, (OCIError *)errhp,
      (OCISession *)sesshp, (ub4) OCI_DEFAULT))
      {
      if (envhp)
      OCIHandleFree((dvoid *)envhp, OCI_HTYPE_ENV);
      return;
      }
      /* Detach from the server */
      if (status = OCIServerDetach((OCIServer *)srvhp, (OCIError *)errhp,
      (ub4)OCI_DEFAULT))
      {
      if (envhp)
      OCIHandleFree((dvoid *)envhp, OCI_HTYPE_ENV);
      return;
      }
      /* Free the handles */
      if (stmthp) OCIHandleFree((dvoid *)stmthp, (ub4) OCI_HTYPE_STMT);
      if (sesshp) OCIHandleFree((dvoid *)sesshp, (ub4) OCI_HTYPE_SESSION);
      if (svchp) OCIHandleFree((dvoid *)svchp, (ub4) OCI_HTYPE_SVCCTX);
      if (srvhp) OCIHandleFree((dvoid *)srvhp, (ub4) OCI_HTYPE_SERVER);
      if (errhp) OCIHandleFree((dvoid *)errhp, (ub4) OCI_HTYPE_ERROR);
      if (envhp) OCIHandleFree((dvoid *)envhp, (ub4) OCI_HTYPE_ENV);
      return;
      }
      void main()
      {
      OCIType *xmltdo;
      xmldocnode *doc;
      ocixmldbparam params[1];
      xmlerr err;
      xmlctx *xctx;
      oratext *ins_stmt;
      sword status;
      xmlnode *root;
      oratext buf[10000];
      /* Initialize envhp, svchp, errhp, dur, stmthp */
      init_oci_connect();
      /* Get an XML context */
      params[0].name_ocixmldbparam = XCTXINIT_OCIDUR;
      params[0].value_ocixmldbparam = &dur;
      xctx = OCIXmlDbInitXmlCtx(envhp, svchp, errhp, params, 1);
      if (!(doc = XmlLoadDom(xctx, &err, "file", filename,
      "schema_location", schemaloc, NULL)))
      {
      printf("Parse failed.\n");
      return;
      }
      else
      printf("Parse succeeded.\n");
      root = XmlDomGetDocElem(xctx, doc);
      printf("The xml document is :\n");
      XmlSaveDom(xctx, &err, (xmlnode *)doc, "buffer", buf, "buffer_length", 10000, NULL);
      printf("%s\n", buf);
      /* Insert the document into my_table */
      ins_stmt = (oratext *)"insert into purchaseorder values (:1)";
      status = OCITypeByName(envhp, errhp, svchp, (const text *) "SYS",
      (ub4) strlen((const char *)"SYS"), (const text *) "XMLTYPE",
      (ub4) strlen((const char *)"XMLTYPE"), (CONST text *) 0,
      (ub4) 0, OCI_DURATION_SESSION, OCI_TYPEGET_HEADER,
      (OCIType **) &xmltdo);
      if (status == OCI_SUCCESS)
      {
      status = exec_bind_xml(svchp, errhp, stmthp, (void *)doc,
      xmltdo, ins_stmt);
      }
      if (status == OCI_SUCCESS)
      printf ("Insert successful\n");
      else
      printf ("Insert failed\n");
      /* Free XML instances */
      if (doc) XmlFreeDocument((xmlctx *)xctx, (xmldocnode *)doc);
      /* Free XML CTX */
      OCIXmlDbFreeXmlCtx(xctx);
      free_oci();
      }