C Oracle队列,是指在Oracle数据库中建立一个队列,为C程序提供数据存放或读取的一种技术。通过C程序调用Oracle内部API,可实现将数据从队列中取出或存入数据到队列中,这将极大的方便某些需要大量数据存储的需要。以下是一个简单的存取C Oracle队列的例子:
#include#include#include#includestatic 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”中,并将其重复存储三遍。接下来,可通过OCI API进行读取操作。
总之,C Oracle队列提供的便利性十分显著,使用前需要配置必要的环境及权限,并规范好字段与类型的定义。