欧美一区二区三区,国内熟女精品熟女A片视频小说,日本av网,小鲜肉男男GAY做受XXX网站

c oracle查詢結果

宋博文1年前7瀏覽0評論

C語言是一門被廣泛應用的編程語言,在數據處理領域有著廣泛的應用。而Oracle數據庫又是一個非常流行的數據庫解決方案,他們之間的結合也讓很多應用系統得以順暢運行。在這種背景下,C語言如何與Oracle數據庫進行交互呢?下面我們來具體探討一下。

首先,我們需要明確的一點就是,查詢結果可能會有多個字段,并且不能確定有多少行數據。那么,我們首先需要定義結構體來存儲這些數據。

typedef struct Person {
int ID;
char name[20];
int age;
char sex[10];
}person;

假設我們要查詢數據庫中的某個表,這個表叫作person_info,我們想知道這張表中的所有數據,我們可以通過以下代碼來實現。

#include#include#includeint main()
{
//初始化Oracle編譯器
OCIEnv* envhp;
OCIError* errhp;
OCIInitialize(OCI_DEFAULT, NULL, NULL, NULL, NULL);
OCIEnvCreate(&envhp, OCI_THREADED, NULL, NULL, NULL, NULL, 0, NULL);
OCIHandleAlloc((dvoid *)envhp, (dvoid **)&errhp, OCI_HTYPE_ERROR, 0, NULL);
//連接數據庫
OCISvcCtx* svchp;
OCIServer* srvhp;
OCISession* usrhp;
OCIHandleAlloc((dvoid *)envhp, (dvoid **)&srvhp, OCI_HTYPE_SERVER, 0, NULL);
OCIHandleAlloc((dvoid *)envhp, (dvoid **)&svchp, OCI_HTYPE_SVCCTX, 0, NULL);
OCIServerAttach(srvhp, errhp, (text *)"ORCL", strlen("ORCL"), OCI_DEFAULT);
OCIAttrSet(svchp, OCI_HTYPE_SVCCTX, (dvoid *)srvhp, 0, OCI_ATTR_SERVER, errhp);
OCIHandleAlloc((dvoid *)envhp, (dvoid **)&usrhp, OCI_HTYPE_SESSION, 0, NULL);
OCIAttrSet((dvoid *)usrhp, (ub4)OCI_HTYPE_SESSION, (dvoid *)"user_name", strlen("user_name"), OCI_ATTR_USERNAME, errhp);
OCIAttrSet((dvoid *)usrhp, (ub4)OCI_HTYPE_SESSION, (dvoid *)"password", strlen("password"), OCI_ATTR_PASSWORD, errhp);
OCISessionBegin(svchp, errhp, usrhp, OCI_CRED_RDBMS, OCI_DEFAULT);
OCIAttrSet((dvoid *)svchp, OCI_HTYPE_SVCCTX, (dvoid *)usrhp, 0, OCI_ATTR_SESSION, errhp);
//定義SQL語句
OCIStmt* stmthp;
OCIHandleAlloc((dvoid *)envhp, (dvoid **)&stmthp,OCI_HTYPE_STMT,0,NULL);
text* sql_query = (text*)"SELECT ID, NAME, AGE, SEX FROM person_info";
//執行SQL查詢
OCIStmtPrepare(stmthp, errhp, sql_query, strlen(sql_query), OCI_NTV_SYNTAX, OCI_DEFAULT);
if (OCIStmtExecute(svchp, stmthp, errhp, 0, 0,NULL,NULL, OCI_DEFAULT) != OCI_SUCCESS)
{
printf("SQL執行錯誤\n");
ErrorHandler(errhp);
}
//存儲查詢的數據
person* pPerson = (person*)malloc(sizeof(person));
OCIDefine* defhp;
OCIDefineByPos(stmthp, &defhp, errhp, 1, &pPerson->ID, sizeof(int), SQLT_INT, NULL, NULL, NULL, OCI_DEFAULT);
OCIDefineByPos(stmthp, &defhp, errhp, 2, (void*)pPerson->name, sizeof(pPerson->name), SQLT_STR, NULL, NULL, NULL, OCI_DEFAULT);
OCIDefineByPos(stmthp, &defhp, errhp, 3, &pPerson->age, sizeof(int), SQLT_INT, NULL, NULL, NULL, OCI_DEFAULT);
OCIDefineByPos(stmthp, &defhp, errhp, 4, (void*)pPerson->sex, sizeof(pPerson->sex), SQLT_STR, NULL, NULL, NULL, OCI_DEFAULT);
//循環輸出查詢結果
while (OCIStmtFetch(stmthp, errhp, 1, OCI_FETCH_NEXT, OCI_DEFAULT) == OCI_SUCCESS)
{
printf("%d %s %d %s\n", pPerson->ID, pPerson->name, pPerson->age, pPerson->sex);
}
//關閉Oracle連接
OCIStmtRelease(stmthp, errhp, NULL, 0, NULL);
OCISessionEnd(svchp, errhp, usrhp, OCI_DEFAULT);
OCIServerDetach(srvhp, errhp, OCI_DEFAULT);
OCIHandleFree((dvoid *)usrhp, OCI_HTYPE_SESSION);
OCIHandleFree((dvoid *)svchp, OCI_HTYPE_SVCCTX);
OCIHandleFree((dvoid *)srvhp, OCI_HTYPE_SERVER);
OCIHandleFree((dvoid *)errhp, OCI_HTYPE_ERROR);
OCIHandleFree((dvoid *)envhp, OCI_HTYPE_ENV);
}

上面的代碼中,我們使用OCIStmtPrepare函數準備SQL語句,使用OCIDefineByPos函數定義查詢結果數據的存儲方式,然后使用OCIStmtFetch函數循環獲取數據,直到沒有可獲取的數據為止。

通過上述代碼的分析,我們可以看到C語言與Oracle進行查詢結果交互還是比較簡單的,關鍵是需要掌握一些底層的API使用。只要我們在實際開發中多加練習,相信我們的技能是會越來越嫻熟的。