Oracle Call Interface (OCI) is a low-level programming interface that allows applications to interact with Oracle Database. It provides a set of C-language functions for managing connections, executing SQL statements, and managing transactions. OCI is commonly used for building high-performance database applications that require direct access to the database.
To use OCI, you need to include the OCI header files in your C program and link with the OCI libraries. You also need to set up a connection to the Oracle database using a connection string that specifies the database name, hostname, port, and user credentials.
Here's an example of using OCI to execute a simple SQL statement:
```
#include <oci.h>
int main()
{
OCIEnv *envhp;
OCIError *errhp;
OCISvcCtx *svchp;
OCIStmt *stmthp;
// Initialize OCI environment
OCIEnvCreate(&envhp, OCI_THREADED, NULL, NULL, NULL, NULL, 0, NULL);
// Initialize error handler
OCIHandleAlloc(envhp, (void **)&errhp, OCI_HTYPE_ERROR, 0, NULL);
// Initialize service context
OCIHandleAlloc(envhp, (void **)&svchp, OCI_HTYPE_SVCCTX, 0, NULL);
OCIAttrSet(svchp, OCI_HTYPE_SVCCTX, server_name, strlen(server_name),
OCI_ATTR_SERVER, errhp);
// Initialize statement
OCIHandleAlloc(envhp, (void **)&stmthp, OCI_HTYPE_STMT, 0, NULL);
OCIStmtPrepare(stmthp, errhp, "SELECT COUNT(*) FROM my_table", strlen("SELECT COUNT(*) FROM my_table"),
OCI_NTV_SYNTAX, OCI_DEFAULT);
// Execute statement
OCIStmtExecute(svchp, stmthp, errhp, 1, 0, NULL, NULL, OCI_DEFAULT);
// Fetch results
int count;
OCIDefine *defhp;
OCIHandleAlloc(envhp, (void **)&defhp, OCI_HTYPE_DEFINE, 0, NULL);
OCIDefineByPos(stmthp, &defhp, errhp, 1, &count, sizeof(count), SQLT_INT, NULL, NULL, NULL, OCI_DEFAULT);
OCIStmtFetch(stmthp, errhp, 1, OCI_FETCH_NEXT, OCI_DEFAULT);
printf("Count: %d\n", count);
// Cleanup
OCIHandleFree(stmthp, OCI_HTYPE_STMT);
OCIHandleFree(svchp, OCI_HTYPE_SVCCTX);
OCIHandleFree(errhp, OCI_HTYPE_ERROR);
OCIHandleFree(envhp, OCI_HTYPE_ENV);
return 0;
}
```
This example shows how to connect to an Oracle database, prepare and execute an SQL statement, fetch the results, and clean up the OCI resources. Note that this is just a simple example, and there are many more OCI functions and options available for building more complex database applications.