<最近在我所在公司,有一個項目需要使用C語言來操作Oracle隊列,因此我也便開始了解了一下C Oracle隊列。 >
C Oracle隊列,是指在Oracle數(shù)據(jù)庫中建立一個隊列,為C程序提供數(shù)據(jù)存放或讀取的一種技術(shù)。通過C程序調(diào)用Oracle內(nèi)部API,可實現(xiàn)將數(shù)據(jù)從隊列中取出或存入數(shù)據(jù)到隊列中,這將極大的方便某些需要大量數(shù)據(jù)存儲的需要。以下是一個簡單的存取C Oracle隊列的例子:
#include#include #include #include static void checkerr(OCIError *errhp, sword status) { sb4 errcode = 0; text errbuf[512]; memset((void*)errbuf, 0, sizeof(errbuf)); if (OCI_ERROR == status) { OCIErrorGet((dvoid *)errhp, (ub4)1, (text *)NULL, &errcode, errbuf, (ub4)sizeof(errbuf), OCI_HTYPE_ERROR); printf("Error - %.*s\n", 512, errbuf); exit(EXIT_FAILURE); } } int main() { OCIEnv *envhp = NULL; OCIError *errhp = NULL; OCIServer *srvhp = NULL; OCISvcCtx *svchp = NULL; OCIStmt *stmthp = NULL; OCIQueue *queue = NULL; sword status; ub2 sqlstmtlen; oratext *sqlstmt = (oratext*)malloc(sizeof(oratext)*512); OCIInit(OCI_THREADED|OCI_OBJECT, (dvoid *)0, (dvoid * (*)(void *, size_t))0, (dvoid * (*)(void *, void *, size_t))0, (void (*)(void *, void *))0 ); OCIHandleAlloc( (dvoid *)NULL, (dvoid **)&envhp, OCI_HTYPE_ENV, (size_t)0, (dvoid **)0); OCIEnvInit((OCIEnv **)&envhp, OCI_DEFAULT, 0, 0 ); OCIHandleAlloc( (dvoid *)envhp, (dvoid **)&errhp, OCI_HTYPE_ERROR, (size_t)0, (dvoid **)0); OCIHandleAlloc( (dvoid *)envhp, (dvoid **)&srvhp, OCI_HTYPE_SERVER, (size_t)0, (dvoid **)0); OCIHandleAlloc( (dvoid *)envhp, (dvoid **)&svchp, OCI_HTYPE_SVCCTX, (size_t)0, (dvoid **)0); OCIHandleAlloc( (dvoid *)envhp, (dvoid **)&stmthp, OCI_HTYPE_STMT, (size_t)0, (dvoid **)0); OCILobLocator* lobloc1 = NULL; OCILobLocator* lobloc2 = NULL; status = OCILogon( envhp, errhp, &svchp, (text *)"system", (ub4)strlen("system"), (text *)"password", (ub4)strlen("password"), (text *)"local", (ub4)strlen("local") ); checkerr(errhp, status); status = OCIServerAttach(srvhp, errhp, (text *)"local", (sb4)strlen("local"), OCI_DEFAULT); checkerr(errhp, status); status = OCIAttrSet((dvoid*) svchp, (ub4) OCI_HTYPE_SVCCTX, (dvoid*) srvhp, (ub4) 0, (ub4) OCI_ATTR_SERVER, errhp); checkerr(errhp, status); status = OCIQueueCreate((dvoid*)svchp, errhp, (CONST OCISvcCtx*)svchp, (CONST oratext*)"sys.app_queue", (ub4)strlen("sys.app_queue"), (CONST OCISubscription** )NULL, (ub4)0, (OCIQueue**)&queue); checkerr(errhp, status); oratext *msg = (oratext*)malloc(sizeof(oratext)*50); memset(msg, 0, 50); strcpy(msg, "this is a test"); status = OCIEnqueue(svchp, errhp, queue, (dvoid*)msg, 13, NULL, NULL); checkerr(errhp, status); status = OCIEnqueue(svchp, errhp, queue, (dvoid*)msg, 13, NULL, NULL); checkerr(errhp, status); status = OCIEnqueue(svchp, errhp, queue, (dvoid*)msg, 13, NULL, NULL); checkerr(errhp, status); status = OCICommit(svchp, errhp, OCI_DEFAULT); checkerr(errhp, status); status = OCIQueueBegin(svchp, errhp, queue, &stmthp); checkerr(errhp, status); status = OCIStmtExecute(svchp, stmthp, errhp, 1, 0, NULL, NULL, OCI_DEFAULT); checkerr(errhp, status); }
上述例子中,將test存儲入Oracle隊列“sys.app_queue”中,并將其重復(fù)存儲三遍。接下來,可通過OCI API進(jìn)行讀取操作。
總之,C Oracle隊列提供的便利性十分顯著,使用前需要配置必要的環(huán)境及權(quán)限,并規(guī)范好字段與類型的定義。
下一篇awr oracle