如何使用C语言操作Oracle数据库(c oracle数据连接)

如何使用C语言操作Oracle数据库

Oracle是一个非常强大的关系型数据库管理系统,而C语言是一种被广泛使用的编程语言。在本文中,我们将介绍如何使用C语言来操作Oracle数据库。

使用OCI库

Oracle公司提供了OCI库给开发者使用。OCI是Oracle Call Interface的缩写,是一个面向过程的C语言API,可以用于连接、操作和管理Oracle数据库。

下面是一个基本的OCI连接Oracle数据库的示例:

#include 
#include
#include
void checkerr(errhp, status)
OCIError *errhp;
sword status;
{
text errbuf[512];
sb4 errcode = 0;
OCIErrorGet((dvoid *)errhp, (ub4)1, (text *)NULL, &errcode,
errbuf, (ub4)sizeof(errbuf), OCI_HTYPE_ERROR);
printf("Error occurred: %.*s\n", 512, errbuf);
exit(1);
}

int mn()
{
OCIEnv *envhp;
OCIError *errhp;
OCIServer *srvhp;
OCISession *sessionhp;
OCISvcCtx *svchp;
sword status;
OCIInitialize(OCI_DEFAULT, (dvoid *)0, (dvoid * (*)(dvoid *, size_t))0,
(dvoid * (*)(dvoid *, dvoid *, size_t))0, (void (*)(dvoid *, dvoid *))0);
OCIEnvInit(&envhp, OCI_DEFAULT, 0, (dvoid **)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);
status = OCIServerAttach(srvhp, errhp, (text *)"dbname", (sb4)strlen("dbname"),
OCI_DEFAULT);
if (status != OCI_SUCCESS)
checkerr(errhp, status);
OCIHandleAlloc((dvoid *)envhp, (dvoid **)&svchp, OCI_HTYPE_SVCCTX,
(size_t)0, (dvoid **)0);
OCIAttrSet((dvoid *)svchp, OCI_HTYPE_SVCCTX, (dvoid *)srvhp, (ub4)0,
OCI_ATTR_SERVER, errhp);
OCIHandleAlloc((dvoid *)envhp, (dvoid **)&sessionhp, OCI_HTYPE_SESSION,
(size_t)0, (dvoid **)0);
OCIAttrSet((dvoid *)sessionhp, OCI_HTYPE_SESSION, (dvoid *)"username",
(ub4)strlen("username"), OCI_ATTR_USERNAME, errhp);
OCIAttrSet((dvoid *)sessionhp, OCI_HTYPE_SESSION, (dvoid *)"password",
(ub4)strlen("password"), OCI_ATTR_PASSWORD, errhp);
status = OCISessionBegin(svchp, errhp, sessionhp, OCI_CRED_RDBMS, OCI_DEFAULT);
if (status != OCI_SUCCESS)
checkerr(errhp, status);
OCIAttrSet((dvoid *)svchp, OCI_HTYPE_SVCCTX, (dvoid *)sessionhp,
(ub4)0, OCI_ATTR_SESSION, errhp);
// use the database here...

OCIHandleFree((dvoid *)sessionhp, OCI_HTYPE_SESSION);
OCIHandleFree((dvoid *)svchp, OCI_HTYPE_SVCCTX);
OCIServerDetach(srvhp, errhp, OCI_DEFAULT);
OCIHandleFree((dvoid *)srvhp, OCI_HTYPE_SERVER);
OCIHandleFree((dvoid *)errhp, OCI_HTYPE_ERROR);
OCIEnvCleanup((dvoid *)envhp);
return 0;
}

以上代码为连接到名为`dbname`的Oracle数据库示例。将示例代码复制并以`test.c`命名,使用以下命令编译:

gcc -o test test.c -I/usr/include/oracle/11.2/client64 -L/usr/lib/oracle/11.2/client64/lib -lclntsh

OCI包含了大量的API可供调用,包括SQL语法、数据类型转换和数据提取等。通过调用`OCIStmtExecute()`和`OCIStmtFetch()`函数,可以轻松地进行SQL查询。

例如,以下代码将从数据库中查询表`employees`中所有员工的名字和雇佣日期,并将结果打印出来:

OCIStmt *stmthp;
OCIParam *colhp;
text *name = NULL;
text *date = NULL;
ub2 name_len = 0;
ub2 date_len = 0;
int i;
OCIHandleAlloc((dvoid *)envhp, (dvoid **)&stmthp, OCI_HTYPE_STMT,
(size_t)0, (dvoid **)0);
status = OCIStmtPrepare(stmthp, errhp,
(text *)"SELECT employee_name, hire_date FROM employees", (ub4)strlen("SELECT employee_name, hire_date FROM employees"),
OCI_NTV_SYNTAX, OCI_DEFAULT);
if (status != OCI_SUCCESS)
checkerr(errhp, status);
status = OCIStmtExecute(svchp, stmthp, errhp, (ub4)0, (ub4)0, (CONST OCISnapshot *)NULL,
(OCISnapshot *)NULL, OCI_DEFAULT);
if (status != OCI_SUCCESS && status != OCI_NO_DATA)
checkerr(errhp, status);
status = OCIStmtFetch(stmthp, errhp, (ub4)1, OCI_FETCH_NEXT, OCI_DEFAULT);
while (status != OCI_NO_DATA)
{
for (i = 1; i
{
status = OCIParamGet(stmthp, OCI_HTYPE_STMT, errhp,
(dvoid **)&colhp, (ub4)i);
if (status != OCI_SUCCESS)
checkerr(errhp, status);
status = OCIAttrGet((dvoid *)colhp, OCI_DTYPE_PARAM,
(dvoid **)&name, (ub4 *)&name_len, OCI_ATTR_NAME, errhp);
if (status != OCI_SUCCESS)
checkerr(errhp, status);
status = OCIAttrGet((dvoid *)colhp, OCI_DTYPE_PARAM,
(dvoid **)&date, (ub4 *)&date_len, OCI_ATTR_NAME, errhp);
if (status != OCI_SUCCESS)
checkerr(errhp, status);
printf("%.*s=%.*s\n", name_len, name, date_len, date);
}
status = OCIStmtFetch(stmthp, errhp, (ub4)1, OCI_FETCH_NEXT, OCI_DEFAULT);
}
OCIHandleFree((dvoid *)stmthp, OCI_HTYPE_STMT);

本次查询结果将会以以下形式打印:

employee_name=John Doe
hire_date=2019-01-01
employee_name=Jane Smith
hire_date=2019-02-01

总结

本文介绍了使用C语言连接和操作Oracle数据库的基本方法。通过OCI库,可以轻松地连接到Oracle数据库、进行SQL查询和数据提取等操作。在应用程序开发过程中,使用OCI库可以为开发者带来更大的灵活性和控制性。


数据运维技术 » 如何使用C语言操作Oracle数据库(c oracle数据连接)