我列出一部分OCI有关的操作数据库的代码。返回的错误写进LOG文件中,在代码后面请了解这方面的大虾解决这个问题:
..............................
typedef struct _DBConnect {
OCIEnv *hpEnviron;
OCIServer *hpServer;
OCISvcCtx *hpSrvCont;
OCIError *hpError;
OCISession *hpSession;
} DBConnect, *pDBConnect;
           ........................
OCIEnv    *hpEnviron = NULL;
OCIServer *hpServer = NULL;
OCISvcCtx *hpSrvCont = NULL;
OCIError  *hpError = NULL;
OCISession *hpSession = NULL; // Allocate Environment Handler of OCI
if( (NewConn = (pDBConnect) GlobalAlloc(
GMEM_FIXED|GPTR|GMEM_ZEROINIT,
sizeof(DBConnect))) == NULL ) {
db_errcode = -1;
sprintf(db_errtext, "Memory Allocation Failed, while connecting DB\n");
return NULL;
} // Initialize the Environment Variable
OCIEnvInit( &hpEnviron, (ub4) OCI_DEFAULT , 0, (dvoid **) 0); if ( hpEnviron == NULL ) {
GlobalFree(NewConn);
        sprintf(db_errtext, "DB Init Failed-Environment Allocation!!");
db_errcode = -2;
return NULL;
} // Allocate Error Handling Handle
OCIHandleAlloc( (dvoid *)hpEnviron, (dvoid **) &hpError, (ub4) OCI_HTYPE_ERROR,
0, (dvoid **) 0); // Allocate Server Handle 
OCIHandleAlloc( (dvoid *)hpEnviron, (dvoid **) &hpServer, (ub4) OCI_HTYPE_SERVER,
0, (dvoid **) 0); // Attach the Server ( Initalize the Server Handle and connect to the server
if ( dbname != NULL && strlen(dbname) > 0 ) {
status = OCIServerAttach( hpServer, hpError, (text *) dbname, (sb4) strlen(dbname), (ub4) OCI_DEFAULT);
}
else {
//MessageBox(ghwndMain, "No DBName", szAppName,MB_YESNO|MB_ICONQUESTION);
status = OCIServerAttach( hpServer, hpError, (text *) 0, (sb4) 0, (ub4) OCI_DEFAULT);
}
 
// Allocate Service Context Handle
OCIHandleAlloc( (dvoid *) hpEnviron, (dvoid **) &hpSrvCont, (ub4) OCI_HTYPE_SVCCTX,
0, (dvoid **) 0);
  
/* set attribute server context in the service context */
status = OCIAttrSet( (dvoid *) hpSrvCont, (ub4) OCI_HTYPE_SVCCTX,
(dvoid *) hpServer, (ub4) 0,
(ub4) OCI_ATTR_SERVER, (OCIError *) hpError);
if ( !DBCheckError(hpError, status) )  {
OCIHandleFree((dvoid *) hpEnviron, (ub4) OCI_HTYPE_ENV);
GlobalFree(NewConn);
return NULL;
} /* allocate a user context handle */
OCIHandleAlloc((dvoid *)hpEnviron, (dvoid **)&hpSession, (ub4) OCI_HTYPE_SESSION,
(size_t) 0, (dvoid **) 0); // Setting User name
status = OCIAttrSet((dvoid *)hpSession, (ub4)OCI_HTYPE_SESSION,
(dvoid *) uname, (ub4)strlen(uname),
OCI_ATTR_USERNAME, hpError);
 
// Setting Password
if( passwd != NULL && strlen(passwd) > 0 ) {
status = OCIAttrSet((dvoid *)hpSession, (ub4)OCI_HTYPE_SESSION,
(dvoid *)passwd, (ub4)strlen(passwd),
OCI_ATTR_PASSWORD, hpError);
}
else {
status = OCIAttrSet((dvoid *)hpSession, (ub4)OCI_HTYPE_SESSION,
(dvoid *)"", (ub4) 0,
OCI_ATTR_PASSWORD, hpError);
}
if ( !DBCheckError(hpError, status) )  {
OCIHandleFree((dvoid *) hpEnviron, (ub4) OCI_HTYPE_ENV);
GlobalFree(NewConn);
return NULL;
} // Begin the user session
status = OCISessionBegin(hpSrvCont, hpError, hpSession, OCI_CRED_RDBMS,
OCI_DEFAULT);
if ( !DBCheckError(hpError, status) )  {
OCIHandleFree((dvoid *) hpEnviron, (ub4) OCI_HTYPE_ENV);
GlobalFree(NewConn);
return NULL;
} // Connect the user session to the Service Context
status = OCIAttrSet((dvoid *)hpSrvCont, (ub4)OCI_HTYPE_SVCCTX,
(dvoid *)hpSession, (ub4)0, OCI_ATTR_SESSION, hpError);
 
/* Allocate a statement handle */
status = OCIHandleAlloc((dvoid *)hpEnviron, (dvoid **) &stmthp,
(ub4)OCI_HTYPE_STMT, (CONST size_t) 0, (dvoid **) 0);
 .......
ZeroMemory(&tmpbinfo, sizeof(BatchInfo));
sprintf(sql, 
" Select "
" a.sponsorid, "
" a.status_time, "
" a.locationid, "
" a.terminalid, "
" a.batchno, "
" a.total_counts, "
" a.total_points, "
" a.total_amounts,"
" a.campaignseq, "
" a.campaigncode, "
" a.campaignpts "
" from batch_info_tbl a "
" where a.status = 'C' "
" order by a.sponsorid, "
" a.locationid, "
" a.terminalid, "
" a.batchno"
); // Allocate a statement handle
status = OCIHandleAlloc((dvoid *)pMyConnect->hpEnviron, 
(dvoid **) &stmthp, (ub4)OCI_HTYPE_STMT, (CONST size_t) 0,
(dvoid **) 0);
 
status = OCIStmtPrepare(stmthp, pMyConnect->hpError, sql,
(ub4)strlen((char *)sql),
(ub4) OCI_NTV_SYNTAX, (ub4) OCI_DEFAULT);
 
status = OCIDefineByPos(stmthp, &defnp[0], pMyConnect->hpError,
1, (ub1 *) tmpbinfo.SponID, 4,
SQLT_CHR, (dvoid *) 0, (ub2 *) 0,
(ub2 *) 0, OCI_DEFAULT);
if (!DBCheckError(pMyConnect->hpError, status) ) {
(void) OCIHandleFree((dvoid *) stmthp, (ub4) OCI_HTYPE_STMT);
WriteLog(MSGPRI_ERR, "ExtractDB 1!!\n");
return FALSE;
}
 
status = OCIDefineByPos(2,3,4,5,6,7,8,9。
   status = OCIDefineByPos(stmthp, &defnp[10], pMyConnect->hpError,
11, (ub1 *) &(tmpbinfo.LuckPnt), sizeof(int),
SQLT_INT, (dvoid *) &(isnull[2]), (ub2 *) 0,
(ub2 *) 0, OCI_DEFAULT);
if (!DBCheckError(pMyConnect->hpError, status) ) {
(void) OCIHandleFree((dvoid *) stmthp, (ub4) OCI_HTYPE_STMT);
WriteLog(MSGPRI_ERR, "ExtractDB 11!!\n");
return FALSE;
}
//modify return 0 instead of 1 joneson sign
status = OCIStmtExecute(pMyConnect->hpSrvCont, stmthp, 
pMyConnect->hpError, (ub4)0, (ub4) 0, (OCISnapshot *)
NULL, (OCISnapshot *) NULL, (ub4) OCI_EXACT_FETCH );
if ( status != OCI_SUCCESS && status != OCI_NO_DATA
&& status != OCI_SUCCESS_WITH_INFO) 
{
 
DBCheckError(pMyConnect->hpError, status);
(void) OCIHandleFree((dvoid *) stmthp, (ub4) OCI_HTYPE_STMT);
WriteLog(MSGPRI_ERR, "ExtractDB StmtExec, %s!!\n", db_errtext);
return FALSE;
}
else if( status == OCI_NO_DATA ) {
// No BatchInfo existed
(void) OCIHandleFree((dvoid *) stmthp, (ub4) OCI_HTYPE_STMT);
WriteLog(MSGPRI_INFO, "No Batch needs to extract!!\n");<-------------------------------------这里出错,然后写到LOG文件里了。
return TRUE;
}, 出错:
2002/06/25 13:08:00 Error ExtractDB StmtExec, Error - ORA-01422: exact fetch returns more than requested number of rows
!!
说指定返回行数多于请求的行数?
ORACLE DOC中指出:
 if you  don't know how many rows to return, just set to 0.
OCIStmtExecute(pMyConnect->hpSrvCont,
                              stmthp, 
      pMyConnect->hpError,
                              (ub4)0,<-----------这里我已经设置为 0了
                             (ub4) 0,
                     (OCISnapshot *)
NULL, (OCISnapshot *) NULL, (ub4) OCI_EXACT_FETCH );
这到底是怎么回事呢?请告诉我, 
[email protected]

解决方案 »

  1.   

    出错地方:
    status = OCIStmtExecute(pMyConnect->hpSrvCont, stmthp, 
            pMyConnect->hpError, (ub4)0, (ub4) 0, (OCISnapshot *)
            NULL, (OCISnapshot *) NULL, (ub4) OCI_EXACT_FETCH );
        if ( status != OCI_SUCCESS && status != OCI_NO_DATA
            && status != OCI_SUCCESS_WITH_INFO) 
        {
         
            DBCheckError(pMyConnect->hpError, status);
            (void) OCIHandleFree((dvoid *) stmthp, (ub4) OCI_HTYPE_STMT);
            WriteLog(MSGPRI_ERR, "ExtractDB StmtExec, %s!!\n", db_errtext);《------------------这里出错
            return FALSE;
        }