- 论坛徽章:
- 0
|
代码如下:
#define BATCHRECORDNUM 5 //批量插入的记录条数
#define DATASIZE 8192 //预分配空间的大小
char ***pBuffer; //数据缓冲区指针
int colNum; //记录字段个数
....
pBuffer = (char ***) malloc(sizeof(char **) * BATCHRECORDNUM);
for(i = 0; i < BATCHRECORDNUM; i++)
{
pBuffer[i] = (char **) malloc(sizeof(char *) * colNum);
}
...
for (Rec = 0; Rec < BATCHRECORDNUM; Rec++)
{
for(i = 0; i < colNum; i++)
{
...
pBuffer[Rec][i] = (char *) malloc(sizeof(char) * DATASIZE);
memset(pBuffer[Rec][i], '\0', sizeof(char) * DATASIZE);
}
}
...
//取数据
for (Rec = 0; Rec< Result.RecordNum; Rec++)
{
for(i = 0; i < colNum; i++)
{
//此处取出数据存入pBuffer[Rec][i]
...
buffsize = DATASIZE - 1;
}
//当读取BATCHRECORDNUM条记录时,导入一次
if(Rec % BATCHRECORDNUM == BATCHRECORDNUM-1)
{
checkerr(myerrhp, OCIHandleAlloc( (dvoid *) myenvhp, (dvoid **) &stmthp, OCI_HTYPE_STMT, 0, 0));
checkerr(myerrhp, OCIStmtPrepare(stmthp, myerrhp, (text *) chInsertTemplate, (ub4)strlen(chInsertTemplate), OCI_NTV_SYNTAX, OCI_DEFAULT));
for(j = 0; j < colNum; j++)
{
checkerr( myerrhp, OCIBindByPos(stmthp, &bndp[j], myerrhp, j + 1, (dvoid *)pBuffer[0][j],
(sword) sizeof(pBuffer[0][j]), colType[j], 0, 0, 0, 0, 0, OCI_DEFAULT));
checkerr(myerrhp, OCIBindArrayOfStruct(bndp[j] , myerrhp, sizeof(pBuffer[0][j]), 0, 0, 0));
}
//!!!此处出错
if((status = OCIStmtExecute(mysvchp, stmthp, myerrhp, (ub4) BATCHRECORDNUM, (ub4) 0, (OCISnapshot *) NULL, (OCISnapshot *) NULL, OCI_DEFAULT)))
{
exporterr("Execute batch insert task error!", __FILE__, (char *)__FUNCTION__, __LINE__);
checkerr(myerrhp, status);
return -1;
}
nImportFlag = 0;
}
}
if(Rec % BATCHRECORDNUM != BATCHRECORDNUM-1 && nImportFlag == 1)
{
checkerr(myerrhp, OCIHandleAlloc( (dvoid *) myenvhp, (dvoid **) &stmthp, OCI_HTYPE_STMT, 0, 0));
checkerr(myerrhp, OCIStmtPrepare(stmthp, myerrhp, (text *) chInsertTemplate, (ub4)strlen(chInsertTemplate), OCI_NTV_SYNTAX, OCI_DEFAULT));
for(j = 0; j < colNum; j++)
{
checkerr( myerrhp, OCIBindByPos(stmthp, &bndp[j], myerrhp, j + 1, (dvoid *)pBuffer[0][j],
(sword) sizeof(pBuffer[0][j]), colType[j], 0, 0, 0, 0, 0, OCI_DEFAULT));
checkerr(myerrhp, OCIBindArrayOfStruct(bndp[j] , myerrhp, sizeof(pBuffer[0][j]), 0, 0, 0));
}
if((status = OCIStmtExecute(mysvchp, stmthp, myerrhp, (ub4) ((Result.RecordNum)%1000), (ub4) 0,
(OCISnapshot *) NULL, (OCISnapshot *) NULL, OCI_DEFAULT)))
{
exporterr("Execute batch insert task error!", __FILE__, (char *)__FUNCTION__, __LINE__);
checkerr(myerrhp, status);
return -1;
}
nImportFlag = 0;
}
/* Commit the change. */
if (status = OCITransCommit(mysvchp, myerrhp, 0))
{
checkerr(myerrhp, status);
}
取数据的部分没有问题,总是执行到OCIStmtExecute报错,判断是OCIBINDBYPOS错误,但是找不到错误在哪里,请教一下高手! |
|