本文將介紹如何使用 C 語言操作 Oracle 數據庫。在實際應用中,我們常常需要對數據進行讀寫操作,并且 Oracle 數據庫在大型應用中被廣泛使用。因此,掌握 C 語言對 Oracle 數據庫的操作技巧是很有必要的。
首先,我們需要先連接到我們的 Oracle 數據庫。這個連接過程可以通過使用 Oracle 提供的 OCI(Oracle Call Interface)庫來實現。以下是一個連接到數據庫的示例代碼:
OCIEnv *envhp; OCIServer *srvhp; OCIError *errhp; OCISession *usrhp; OCIEnvCreate(&envhp, OCI_ENV_DEFAULT, 0, NULL, NULL, NULL, 0, NULL); OCIHandleAlloc(envhp, (void **)&srvhp, OCI_HTYPE_SERVER, 0, NULL); OCIHandleAlloc(envhp, (void **)&errhp, OCI_HTYPE_ERROR, 0, NULL); OCIServerAttach(srvhp, errhp, (text *)"dbname", strlen("dbname"), OCI_DEFAULT); OCIHandleAlloc(envhp, (void **)&usrhp, OCI_HTYPE_SESSION, 0, NULL); OCIAttrSet(usrhp, OCI_HTYPE_SESSION, (void *)"username", strlen("username"), OCI_ATTR_USERNAME, errhp); OCIAttrSet(usrhp, OCI_HTYPE_SESSION, (void *)"password", strlen("password"), OCI_ATTR_PASSWORD, errhp); OCISessionBegin(srvhp, errhp, usrhp, OCI_CRED_RDBMS, OCI_DEFAULT); OCISessionEnd(srvhp, errhp, usrhp, OCI_DEFAULT); OCIServerDetach(srvhp, errhp, OCI_DEFAULT); OCIHandleFree(usrhp, OCI_HTYPE_SESSION); OCIHandleFree(srvhp, OCI_HTYPE_SERVER); OCIHandleFree(errhp, OCI_HTYPE_ERROR); OCIHandleFree(envhp, OCI_HTYPE_ENV);
接下來,我們需要使用 SQL 語言來對數據庫進行操作。在 C 語言中,我們可以使用 OCIStmtPrepare、OCIStmtExecute 和 OCIStmtFetch 等函數來執行 SQL 語句。例如,以下是一段 C 代碼,可以執行一條查詢語句并輸出結果:
OCIStmt *stmthp; OCIParam *paramhp; ub4 rowcount; char sqlstmt[256]; sprintf(sqlstmt, "SELECT * FROM mytable WHERE id = :id"); OCIHandleAlloc(envhp, (void **)&stmthp, OCI_HTYPE_STMT, 0, NULL); OCIStmtPrepare(stmthp, errhp, (text *)sqlstmt, strlen(sqlstmt), OCI_NTV_SYNTAX, OCI_DEFAULT); OCIParamGet(stmthp, OCI_HTYPE_STMT, errhp, (void **)¶mhp, 1); OCINumber y; OCINumberFromInt(errhp, &y, sizeof(int), OCI_NUMBER_SIGNED, &id); OCIAttrSet(paramhp, OCI_DTYPE_PARAM, &y, sizeof(y), OCI_ATTR_VALUE, errhp); OCIExecute(stmthp, errhp, OCI_DEFAULT); OCIAttrGet(stmthp, OCI_HTYPE_STMT, &rowcount, sizeof(rowcount), OCI_ATTR_ROW_COUNT, errhp); OCIStmtFetch(stmthp, errhp, 1, OCI_FETCH_NEXT, OCI_DEFAULT); int col1, col2; OCIAttrGet(stmthp, OCI_HTYPE_STMT, &col1, sizeof(col1), OCI_ATTR_DATA_TYPE, errhp); OCIAttrGet(stmthp, OCI_HTYPE_STMT, &col2, sizeof(col2), OCI_ATTR_DATA_TYPE, errhp); printf("Result: %d %d", col1, col2); OCIHandleFree(stmthp, OCI_HTYPE_STMT);
除了查詢操作,我們還可以通過使用 OCIStmtPrepare、OCIStmtExecute 和 OCIStmtFetch 等函數來執行增加、修改和刪除等操作。以下是一段 C 代碼,可以執行一條插入語句:
OCIStmt *stmthp; OCIParam *paramhp; ub4 rowcount; char sqlstmt[256]; sprintf(sqlstmt, "INSERT INTO mytable (col1, col2) VALUES (:c1, :c2)"); OCIHandleAlloc(envhp, (void **)&stmthp, OCI_HTYPE_STMT, 0, NULL); OCIStmtPrepare(stmthp, errhp, (text *)sqlstmt, strlen(sqlstmt), OCI_NTV_SYNTAX, OCI_DEFAULT); OCIParamGet(stmthp, OCI_HTYPE_STMT, errhp, (void **)¶mhp, 1); OCINumber y; OCINumberFromInt(errhp, &y, sizeof(int), OCI_NUMBER_SIGNED, &c1); OCIAttrSet(paramhp, OCI_DTYPE_PARAM, &y, sizeof(y), OCI_ATTR_VALUE, errhp); OCIParamGet(stmthp, OCI_HTYPE_STMT, errhp, (void **)¶mhp, 2); OCINumber z; OCINumberFromInt(errhp, &z, sizeof(int), OCI_NUMBER_SIGNED, &c2); OCIAttrSet(paramhp, OCI_DTYPE_PARAM, &z, sizeof(z), OCI_ATTR_VALUE, errhp); OCIExecute(stmthp, errhp, OCI_DEFAULT); OCIAttrGet(stmthp, OCI_HTYPE_STMT, &rowcount, sizeof(rowcount), OCI_ATTR_ROW_COUNT, errhp); OCIHandleFree(stmthp, OCI_HTYPE_STMT);
綜上所述,使用 C 語言進行 Oracle 數據庫操作需要使用 OCI 庫提供的函數來完成連接和執行 SQL 語句等基本操作。我們可以通過代碼中的示例來理解這些函數的用法,并進行相應的應用。