文档中心MogDBMogDB StackUqbar
v5.0

文档:v5.0

支持的版本:

其他版本:

示例:常用功能和批量绑定

常用功能示例代码

void test()
{
 const char *dbname;
 const char *user;
 const char *pwd;
 int   teststep[3] = {0};
 sword  result, result1, result2;
 T_OCI_ENV OCIEnv;
 T_OCI_ENV  *pOCIEnv;
 T_DB_Info dbInfo = {0};

 GetTestConnectInfo(&user, &pwd, &dbname);
 strcpy(dbInfo.username, user);
 strcpy(dbInfo.password, pwd);
 strcpy(dbInfo.service_name, dbname);
 InitOCIEnv(&OCIEnv);
 pOCIEnv = &OCIEnv;

 ConnectDB(&OCIEnv, &dbInfo);
 
 char *szSQL = "create table t_def6(col1 integer, col2 varchar(64), col3 varchar(64), col4 varchar(64), col5 timestamp, col6 timestamp)";
 OCIStmtPrepare(pOCIEnv->stmthp, pOCIEnv->errhp, (OraText *) szSQL, (ub4) strlen((char *) szSQL), (ub4) OCI_NTV_SYNTAX, (ub4) OCI_DEFAULT);
 OCIAttrSet((dvoid *) pOCIEnv->stmthp, (ub4) OCI_HTYPE_STMT, (dvoid *) & prefetch, 0, (ub4) OCI_ATTR_PREFETCH_ROWS, pOCIEnv->errhp);
 
 #define MAX_LEN 63
 sword  prefetch = 1;
 int   col1 = 0;
 char  col2[100][16] = {0};
 char  col3[100][16] = {0};
 char  col4[100][16] = {0};
 OCIDate  col5[2] = {{0}};
 OCIDate  col6[2] = {{0}};

 szSQL = (text *) "SELECT col1, col2, col3, col4, col5, col6  FROM t_def6 where col1=:col1";
 sb2   indicator1; 
 sb2   indicator2;
 sb2   indicator3;
 sb2   indicator4;
 sb2   indicator5;
 sb2   indicator6;
 OCIDefine   def1 = {0};
 OCIDefine   def2 = {0};
 OCIDefine   def3 = {0};
 OCIDefine   def4 = {0}; 
 OCIDefine   def5 = {0};
 OCIDefine   def6 = {0}; 
 OCIDefine  *defn2p = (OCIDefine *) &def2;
 OCIDefine  *defn3p = (OCIDefine *) &def3;
 OCIDefine  *defn4p = (OCIDefine *) &def4;
 OCIDefine  *defn5p = (OCIDefine *) &def5;
 OCIDefine  *defn6p = (OCIDefine *) &def6;

 OCIHandleAlloc((dvoid *) pOCIEnv->envhp, (dvoid * *) & pOCIEnv->stmthp, OCI_HTYPE_STMT, (size_t) 0, (dvoid * *) 0);

 OCIStmtPrepare(pOCIEnv->stmthp, pOCIEnv->errhp, (OraText *) szSQL, (ub4) strlen((char *) szSQL), (ub4) OCI_NTV_SYNTAX, (ub4) OCI_DEFAULT);
 OCIAttrSet((dvoid *) pOCIEnv->stmthp, (ub4) OCI_HTYPE_STMT, (dvoid *) & prefetch, 0, (ub4) OCI_ATTR_PREFETCH_ROWS, pOCIEnv->errhp);
 
 int nVal = 1;
 OCIBindByName(pOCIEnv->stmthp,&bindpp,pOCIEnv->errhp,(text *) ":col1",(sb4) strlen(":col1"),(dvoid *) & nVal,sizeof(nVal),SQLT_INT,(dvoid *) 0,(ub2 *) 0,(ub2 *) 0,(ub4) 0,(ub4 *) 0,OCI_DEFAULT);
         
 result = OCIDefineByPos(pOCIEnv->stmthp, &defn1p, pOCIEnv->errhp, 1, (dvoid * ) col1, (sword) sizeof(int), SQLT_INT, (dvoid *) &indicator1, (ub2 *) 0, (ub2 *) 0, OCI_DEFAULT);
 result = OCIDefineByPos(pOCIEnv->stmthp, &defn2p, pOCIEnv->errhp, 2, (dvoid * *) col2, (sword) LEN_16, SQLT_STR, (dvoid *) &indicator2, (ub2 *) 0, (ub2 *) 0, OCI_DEFAULT);
 result = OCIDefineByPos(pOCIEnv->stmthp, &defn1p, pOCIEnv->errhp, 3, (dvoid * *) col3, (sword) LEN_16, SQLT_STR, (dvoid *) &indicator3, (ub2 *) 0, (ub2 *) 0, OCI_DEFAULT);
 result = OCIDefineByPos(pOCIEnv->stmthp, &defn2p, pOCIEnv->errhp, 4, (dvoid * *) col4, (sword) LEN_16, SQLT_STR, (dvoid *) &indicator4, (ub2 *) 0, (ub2 *) 0, OCI_DEFAULT);
 result = OCIDefineByPos(pOCIEnv->stmthp, &defn3p, pOCIEnv->errhp, 5, (dvoid *) col5, (sword) sizeof(OCIDate), SQLT_ODT, (dvoid *) &indicator5, (ub2 *) 0, (ub2 *) 0, OCI_DEFAULT); 
 result = OCIDefineByPos(pOCIEnv->stmthp, &defn4p, pOCIEnv->errhp, 6, (dvoid *) col6, (sword) sizeof(OCIDate), SQLT_ODT, (dvoid *) &indicator6, (ub2 *) 0, (ub2 *) 0, OCI_DEFAULT);

 result = OCIStmtExecute(pOCIEnv->svchp, pOCIEnv->stmthp, pOCIEnv->errhp, (ub4) 0, (ub4) 0, (CONST OCISnapshot *) NULL, (OCISnapshot *) NULL, OCI_COMMIT_ON_SUCCESS);
 
 int tuple_num = 0;
 while (TRUE)
 {
  result = OCIStmtFetch2(pOCIEnv->stmthp, pOCIEnv->errhp, (ub4) 1, (ub2) OCI_FETCH_NEXT, 0, (ub4) OCI_DEFAULT);
  if (result == OCI_SUCCESS && tuple_num % 100000 == 0)
  {
   printf("tuple_num=%d, col2=%s, col3=%s, col4=%s.\n",  tuple_num,  col2[0], col3[0], col4[0]);
  }
  if (result == OCI_NO_DATA) {
   break;
  }
  else {
   printf("test_mem failed.\n");
   break;
  }

  tuple_num++;
 }

 szSQL = "drop table t_def6";
 OCIStmtPrepare(pOCIEnv->stmthp, pOCIEnv->errhp, (OraText *) szSQL, (ub4) strlen((char *) szSQL), (ub4) OCI_NTV_SYNTAX, (ub4) OCI_DEFAULT);
 OCIStmtExecute(pOCIEnv->svchp, pOCIEnv->stmthp, pOCIEnv->errhp, (ub4) 1, (ub4) 0, (CONST OCISnapshot *) NULL, (OCISnapshot *) NULL, OCI_COMMIT_ON_SUCCESS);
  
 result = OCIStmtRelease(pOCIEnv->stmthp, pOCIEnv->errhp, NULL, 0, 0);
 result = OCISessionEnd(pOCIEnv->svchp, pOCIEnv->errhp, pOCIEnv->usrhp, OCI_DEFAULT);
 result = OCIServerDetach(pOCIEnv->srvhp, pOCIEnv->errhp, OCI_DEFAULT); 
 
 OCIHandleFree(pOCIEnv, OCI_HTYPE_SERVER);
 OCIHandleFree(pOCIEnv, OCI_HTYPE_SVCCTX);
 OCIHandleFree(pOCIEnv, OCI_HTYPE_ERROR);
 OCIHandleFree(pOCIEnv, OCI_HTYPE_SESSION);

 printf("test_mem success.\n");
}
Copyright © 2011-2025 www.enmotech.com All rights reserved.