⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ocitest.cpp

📁 oci函数批量插入数据
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		memset((void *)in9[i], (int) 'G', (size_t) in9_SIZE);
		in9[i][in9_SIZE] = '\0';
		in10[i] = 10 + i;
		in11[i] = 11 + i;
		in12[i] = 12 + i;
		in13[i] = 13 + i;
		in14[i] = 14 + i;
		memset((void *)in15[i], (int) 'H', (size_t) in15_SIZE);
		in15[i][in15_SIZE] = '\0';
		in15[i][in15_SIZE-1] = '\0';
		in16[i] = 16 + i;
		memset((void *)in17[i], (int) 'I', (size_t) in17_SIZE);
		in17[i][in17_SIZE] = '\0';
		in17[i][in17_SIZE-1] = '\0';
		in18[i] = 18 + i;
		memset((void *)in19[i], (int) 'J', (size_t) in19_SIZE);
		in19[i][in19_SIZE] = '\0';
		in19[i][in19_SIZE-1] = '\0';
		in20[i] = 20 + i;
		memset((void *)in21[i], (int) 'K', (size_t) in21_SIZE);
		in21[i][in21_SIZE] = '\0';
		in21[i][in21_SIZE-1] = '\0';
		memset((void *)in22[i], (int) 'L', (size_t) in22_SIZE);
		in22[i][in22_SIZE] = '\0';
		in22[i][in22_SIZE-1] = '\0';
		in23[i] = 23 + i;
		memset((void *)in24[i], (int) 'M', (size_t) in24_SIZE);
		in24[i][in24_SIZE] = '\0';
		in24[i][in24_SIZE-1] = '\0';
		memset((void *)in25[i], (int) 'N', (size_t) in25_SIZE);
		in25[i][in25_SIZE] = '\0';
		memset((void *)in26[i], (int) 'O', (size_t) in26_SIZE);
		in26[i][in26_SIZE] = '\0';
		memset((void *)in27[i], (int) 'P', (size_t) in27_SIZE);
		in27[i][in27_SIZE] = '\0';
		in27[i][in27_SIZE-1] = '\0';
		memset((void *)in28[i], (int) 'Q', (size_t) in28_SIZE);
		in28[i][in28_SIZE] = '\0';
		in28[i][in28_SIZE-1] = '\0';
		memset((void *)in29[i], (int) 'R', (size_t) in29_SIZE);
		in29[i][in29_SIZE] = '\0';
		in29[i][in29_SIZE-1] = '\0';
		memset((void *)in30[i], (int) 'S', (size_t) in30_SIZE);
		in30[i][in30_SIZE] = '\0';
		in31[i] = 31 + i;
		memset((void *)in32[i], (int) 'T', (size_t) in32_SIZE);
		in32[i][in32_SIZE] = '\0';
		in33[i] = 33 + i;
		memset((void *)in34[i], (int) 'U', (size_t) in34_SIZE);
		in34[i][in34_SIZE] = '\0';
		memset((void *)in35[i], (int) 'V', (size_t) in35_SIZE);
		in35[i][in35_SIZE] = '\0';
		memset((void *)in36[i], (int) 'W', (size_t) in36_SIZE);
		in36[i][in36_SIZE] = '\0';
		memset((void *)in37[i], (int) 'X', (size_t) in37_SIZE);
		in37[i][in37_SIZE] = '\0';
		memset((void *)in38[i], (int) 'Y', (size_t) in38_SIZE);
		in38[i][in38_SIZE] = '\0';

		char * pSql = new char[MAX_LEN];
		memset(pSql, 0, MAX_LEN);

		const char *sqlstr = "INSERT INTO TOF_JJZH VALUES('%s', '%s', '%s', %d, "
			"'%s', '%s', '%s', %d, '%s', %d, %d, %d, %d, %d, '%s', %d, '%s', "
			"%d, '%s', %d, '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', "
			"'%s', '%s', %d, '%s', %d, '%s', '%s', '%s', '%s', '%s')";

		sprintf(pSql, sqlstr, in1[i], in2[i], in3[i], in4[i], in5[i], in6[i], 
			in7[i], in8[i], in9[i], in10[i], in11[i], in12[i], in13[i], in14[i],
			in15[i], in16[i], in17[i], in18[i], in19[i], in20[i], in21[i], in22[i], 
			in23[i], in24[i], in25[i], in26[i], in27[i], in28[i], in29[i], in30[i], 
			in31[i], in32[i], in33[i], in34[i], in35[i], in36[i], in37[i], in38[i]);

		delete [] pSql;
	}

}

void BulkLoad()
{
	OCIEnv *pEnv;
	OCIError *pError;
	OCISvcCtx *pSvcCtx;
	OCIServer *pServer;
	OCISession *pSession;
	OCIStmt *pStmt;
	sb4 m_nErrCode;
	text m_strErrBuf[ERRBUF_LEN];
	unsigned int nIndex, nLastIndex;
	unsigned int nBaseTickCount, nTickCount, nLastTickCount;

	sword nOciRet = 0;
	OCIBind *bndhp[37];

	nOciRet = OCIEnvCreate(&pEnv, OCI_THREADED | OCI_OBJECT, NULL, NULL, NULL, NULL, 0, NULL);
	nOciRet = OCIHandleAlloc(pEnv, (void **) &pError, OCI_HTYPE_ERROR, 0, NULL);
	nOciRet = OCIHandleAlloc(pEnv, (void **) &pSvcCtx, OCI_HTYPE_SVCCTX, 0, NULL);

	nOciRet = OCIHandleAlloc(pEnv, (void **) &pServer, OCI_HTYPE_SERVER, 0, NULL);
	nOciRet = OCIServerAttach(pServer, pError, (unsigned char *) "lgjora", strlen("lgjora"), OCI_DEFAULT);
	nOciRet = OCIAttrSet(pSvcCtx, OCI_HTYPE_SVCCTX, pServer, 0, OCI_ATTR_SERVER, pError);

	nOciRet = OCIHandleAlloc(pEnv, (void **) &pSession, OCI_HTYPE_SESSION, 0, NULL);
	nOciRet = OCIAttrSet(pSession, OCI_HTYPE_SESSION, "test", strlen("test"), OCI_ATTR_USERNAME, pError);
	nOciRet = OCIAttrSet(pSession, OCI_HTYPE_SESSION, "test", strlen("test"), OCI_ATTR_PASSWORD, pError);
	nOciRet = OCISessionBegin(pSvcCtx, pError, pSession, OCI_CRED_RDBMS, OCI_DEFAULT);
	nOciRet = OCIAttrSet(pSvcCtx, OCI_HTYPE_SVCCTX, pSession, 0, OCI_ATTR_SESSION, pError);

	text *sqlstmt = (text *)"INSERT INTO TOF_JJZH VALUES (:1, :2, :3, :4, :5, "
							":6, :7, :8, :9, :10, :11, :12, :13, :14, :15, :16, "
							":17, :18, :19, :20, :21, :22, :23, :24, :25, :26, "
							":27, :28, :29, :30, :31, :32, :33, :34, :35, :36, "
							":37, :38)";

	nOciRet = OCIHandleAlloc(pEnv, (void **) &pStmt, OCI_HTYPE_STMT, 0, NULL);
	nOciRet = OCIStmtPrepare(pStmt, pError, sqlstmt, (ub4)strlen((char *)sqlstmt), OCI_NTV_SYNTAX, OCI_DEFAULT);

	nOciRet = bind_pos(pStmt, bndhp, pError);
	nOciRet = bind_array(bndhp, pError);

	nLastIndex = 0;
	nLastTickCount = GetTickCount();
	nBaseTickCount = nLastTickCount;
	
	for (nIndex = 0; nIndex < 10000000; )
	{
		getdata( nIndex );		

		nOciRet = OCIStmtExecute(pSvcCtx, pStmt, pError, (ub4) ROW_COUNT, (ub4) 0, 
						(CONST OCISnapshot*) 0, (OCISnapshot*) 0, 
						(ub4) OCI_DEFAULT);

		if (nOciRet)
		{
			OCIErrorGet( pError, 1, NULL, &m_nErrCode, m_strErrBuf,	ERRBUF_LEN, OCI_HTYPE_ERROR );
			printf("Insert is failed! reason:%s\n", (char *)m_strErrBuf);
		}
		
		nOciRet = OCITransCommit(pSvcCtx, pError, 0);
		if (nOciRet)
			printf("Commit is failed!\n");
		
		nIndex += ROW_COUNT;

		nTickCount = GetTickCount();
		if(nTickCount >= (nLastTickCount + 1000))
		{
			printf("total %d, time %d, step %d, step time %d, speed %d\r\n",
				nIndex, 
				nTickCount - nBaseTickCount, 
				nIndex - nLastIndex, 
				nTickCount - nLastTickCount, 
				(nIndex - nLastIndex) * 1000 / (nTickCount - nLastTickCount));

			nLastIndex = nIndex;
			nLastTickCount = nTickCount;
		}
	}

	nOciRet = OCIHandleFree(pStmt, OCI_HTYPE_STMT);
}
/*
#define ROW_COUNT 2000
#define C1_SIZE 1024

char c1[ROW_COUNT][C1_SIZE];

void BulkLoad()
{
	OCIEnv *pEnv;
	OCIError *pError;
	OCISvcCtx *pSvcCtx;
	OCIServer *pServer;
	OCISession *pSession;
	OCIStmt *pStmt;
	OCIBind *pBind;
	unsigned int nIndex, nLastIndex;
	unsigned int nBaseTickCount, nTickCount, nLastTickCount;

	OCIEnvCreate(&pEnv, OCI_THREADED | OCI_OBJECT, NULL, NULL, NULL, NULL, 0, NULL);
	OCIHandleAlloc(pEnv, (void **) &pError, OCI_HTYPE_ERROR, 0, NULL);
	OCIHandleAlloc(pEnv, (void **) &pSvcCtx, OCI_HTYPE_SVCCTX, 0, NULL);

	OCIHandleAlloc(pEnv, (void **) &pServer, OCI_HTYPE_SERVER, 0, NULL);
	OCIServerAttach(pServer, pError, (unsigned char *) "yp", strlen("yp"), OCI_DEFAULT);
	OCIAttrSet(pSvcCtx, OCI_HTYPE_SVCCTX, pServer, 0, OCI_ATTR_SERVER, pError);

	OCIHandleAlloc(pEnv, (void **) &pSession, OCI_HTYPE_SESSION, 0, NULL);
	OCIAttrSet(pSession, OCI_HTYPE_SESSION, "yp", strlen("yp"), OCI_ATTR_USERNAME, pError);
	OCIAttrSet(pSession, OCI_HTYPE_SESSION, "yipeng", strlen("yipeng"), OCI_ATTR_PASSWORD, pError);
	OCISessionBegin(pSvcCtx, pError, pSession, OCI_CRED_RDBMS, OCI_DEFAULT);
	OCIAttrSet(pSvcCtx, OCI_HTYPE_SVCCTX, pSession, 0, OCI_ATTR_SESSION, pError);

	OCIHandleAlloc(pEnv, (void **) &pStmt, OCI_HTYPE_STMT, 0, NULL);
	OCIStmtPrepare(pStmt, pError, (unsigned char *) "INSERT INTO yp VALUES (:1)", strlen("INSERT INTO yp VALUES (:1)"), OCI_NTV_SYNTAX, OCI_DEFAULT);
	OCIBindByPos(pStmt, &pBind, pError, 1, c1[0], C1_SIZE, SQLT_CHR, NULL, NULL, NULL, 0, NULL, OCI_DEFAULT);
	OCIBindArrayOfStruct(pBind, pError, C1_SIZE, 0, 0, 0);

	nLastIndex = 0;
	nLastTickCount = GetTickCount();
	nBaseTickCount = nLastTickCount;
	for(nIndex = 0; nIndex < 10000000; )
	{
		memset(c1, '1', sizeof(c1));
		OCIStmtExecute(pSvcCtx, pStmt, pError, ROW_COUNT, 0, NULL, NULL, OCI_DEFAULT);
		OCITransCommit(pSvcCtx, pError, 0);

		nIndex += ROW_COUNT;

		nTickCount = GetTickCount();
		if(nTickCount >= (nLastTickCount + 1000))
		{
			printf("total %d, time %d, step %d, step time %d, speed %d\r\n", nIndex, nTickCount - nBaseTickCount, nIndex - nLastIndex, nTickCount - nLastTickCount, (nIndex - nLastIndex) * 1000 / (nTickCount - nLastTickCount));

			nLastIndex = nIndex;
			nLastTickCount = nTickCount;
		}
	}

	OCIHandleFree(pStmt, OCI_HTYPE_STMT);
}

void DirectLoad()
{
	OCIEnv *pEnv;
	OCIError *pError;
	OCISvcCtx *pSvcCtx;
	OCIServer *pServer;
	OCISession *pSession;
	OCIDirPathCtx *pDirPathCtx;
	ub1 nNoLog;
	ub2 nColCount;
	OCIParam *pParamColList, *pParamCol;
	ub2 nColType;
	ub4 nColSize;
	OCIDirPathColArray *pDirPathColArray;
	OCIDirPathStream *pDirPathStream;
	unsigned int nLoadCount;
	unsigned int nLoad;
	unsigned int nArraySize;
	unsigned int nArrayIndex;
	unsigned int nConvertCount;
	unsigned int nConvert;
	unsigned int nIndex, nLastIndex;
	unsigned int nBaseTickCount, nTickCount, nLastTickCount;

	OCIEnvCreate(&pEnv, OCI_THREADED | OCI_OBJECT, NULL, NULL, NULL, NULL, 0, NULL);
	OCIHandleAlloc(pEnv, (void **) &pError, OCI_HTYPE_ERROR, 0, NULL);
	OCIHandleAlloc(pEnv, (void **) &pSvcCtx, OCI_HTYPE_SVCCTX, 0, NULL);

	OCIHandleAlloc(pEnv, (void **) &pServer, OCI_HTYPE_SERVER, 0, NULL);
	OCIServerAttach(pServer, pError, (unsigned char *) "yp", strlen("yp"), OCI_DEFAULT);
	OCIAttrSet(pSvcCtx, OCI_HTYPE_SVCCTX, pServer, 0, OCI_ATTR_SERVER, pError);

	OCIHandleAlloc(pEnv, (void **) &pSession, OCI_HTYPE_SESSION, 0, NULL);
	OCIAttrSet(pSession, OCI_HTYPE_SESSION, "yp", strlen("yp"), OCI_ATTR_USERNAME, pError);
	OCIAttrSet(pSession, OCI_HTYPE_SESSION, "yipeng", strlen("yipeng"), OCI_ATTR_PASSWORD, pError);
	OCISessionBegin(pSvcCtx, pError, pSession, OCI_CRED_RDBMS, OCI_DEFAULT);
	OCIAttrSet(pSvcCtx, OCI_HTYPE_SVCCTX, pSession, 0, OCI_ATTR_SESSION, pError);

	OCIHandleAlloc(pEnv, (void **) &pDirPathCtx, OCI_HTYPE_DIRPATH_CTX, 0, NULL);
	OCIAttrSet(pDirPathCtx, OCI_HTYPE_DIRPATH_CTX, "yp", strlen("yp"), OCI_ATTR_NAME, pError);
	OCIAttrSet(pDirPathCtx, OCI_HTYPE_DIRPATH_CTX, "yp", strlen("yp"), OCI_ATTR_SCHEMA_NAME, pError);
	nNoLog = 1;
	OCIAttrSet(pDirPathCtx, OCI_HTYPE_DIRPATH_CTX, &nNoLog, 0, OCI_ATTR_DIRPATH_NOLOG, pError);
	nColCount = 1;
	OCIAttrSet(pDirPathCtx, OCI_HTYPE_DIRPATH_CTX, &nColCount, 0, OCI_ATTR_NUM_COLS, pError);
	OCIAttrGet(pDirPathCtx, OCI_HTYPE_DIRPATH_CTX, &pParamColList, 0, OCI_ATTR_LIST_COLUMNS, pError);
	OCIParamGet(pParamColList, OCI_DTYPE_PARAM, pError, (void **) &pParamCol, 1);
	OCIAttrSet(pParamCol, OCI_DTYPE_PARAM, "c1", strlen("c1"), OCI_ATTR_NAME, pError);
	nColType = SQLT_CHR;
	OCIAttrSet(pParamCol, OCI_DTYPE_PARAM, &nColType, 0, OCI_ATTR_DATA_TYPE, pError);
	nColSize = C1_SIZE;
	OCIAttrSet(pParamCol, OCI_DTYPE_PARAM, &nColSize, 0, OCI_ATTR_DATA_SIZE, pError);
	OCIDescriptorFree(pParamCol, OCI_DTYPE_PARAM);
	OCIDirPathPrepare(pDirPathCtx, pSvcCtx, pError);
	OCIHandleAlloc(pDirPathCtx, (void **) &pDirPathColArray, OCI_HTYPE_DIRPATH_COLUMN_ARRAY, 0, NULL);
	OCIAttrGet(pDirPathColArray, OCI_HTYPE_DIRPATH_COLUMN_ARRAY, &nArraySize, 0, OCI_ATTR_NUM_ROWS, pError);
	OCIHandleAlloc(pDirPathCtx, (void **) &pDirPathStream, OCI_HTYPE_DIRPATH_STREAM, 0, NULL);

	nLastIndex = 0;
	nLastTickCount = GetTickCount();
	nBaseTickCount = nLastTickCount;
	for(nIndex = 0; nIndex < 10000000; )
	{
		memset(c1, '1', sizeof(c1));
		for(nLoadCount = 0; nLoadCount < ROW_COUNT; nLoadCount += nLoad)
		{
			nLoad = ROW_COUNT - nLoadCount;
			if(nLoad > nArraySize)
			{
				nLoad = nArraySize;
			}

			OCIDirPathColArrayReset(pDirPathColArray, pError);
			for(nArrayIndex = 0; nArrayIndex < nLoad; nArrayIndex ++)
			{
				OCIDirPathColArrayEntrySet(pDirPathColArray, pError, nArrayIndex, 0, (unsigned char *) c1[nArrayIndex], C1_SIZE, OCI_DIRPATH_COL_COMPLETE);
			}

			for(nConvertCount = 0; nConvertCount < nLoad; nConvertCount += nConvert)
			{
				OCIDirPathStreamReset(pDirPathStream, pError);
				OCIDirPathColArrayToStream(pDirPathColArray, pDirPathCtx, pDirPathStream, pError, nLoad - nConvertCount, 0);
				OCIAttrGet(pDirPathColArray, OCI_HTYPE_DIRPATH_COLUMN_ARRAY, &nConvert, 0, OCI_ATTR_ROW_COUNT, pError);
				OCIDirPathLoadStream(pDirPathCtx, pDirPathStream, pError);
			}
		}

		nIndex += ROW_COUNT;

		nTickCount = GetTickCount();
		if(nTickCount >= (nLastTickCount + 1000))
		{
			printf("total %d, time %d, step %d, step time %d, speed %d\r\n", nIndex, nTickCount - nBaseTickCount, nIndex - nLastIndex, nTickCount - nLastTickCount, (nIndex - nLastIndex) * 1000 / (nTickCount - nLastTickCount));

			nLastIndex = nIndex;
			nLastTickCount = nTickCount;
		}
	}

	OCIDirPathDataSave(pDirPathCtx, pError, OCI_DIRPATH_DATASAVE_FINISH);
	OCIDirPathFinish(pDirPathCtx, pError);
}
*/
int main(int argc, char* argv[])
{
	BulkLoad();
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -