C语言操作Oracle数据库的开发技巧(c 操作oracle开发)

C语言是一种强大的编程语言,可以用于开发各种类型的应用,包括与Oracle数据库的交互。在本文中,我们将探讨一些C语言操作Oracle数据库的开发技巧和实践经验。

1. 安装和配置Oracle客户端和ODBC驱动程序

要使用C语言操作Oracle数据库,您需要先安装和配置Oracle客户端和ODBC驱动程序。Oracle客户端包括SQL Plus、Oracle Net和JDBC等工具,可以让您与Oracle数据库进行交互。ODBC驱动程序是一个开放式的接口,与大多数数据库兼容,用于管理与程序之间的数据传输。

在Windows操作系统中,您可以像下面这样安装和配置Oracle客户端和ODBC驱动程序:

1. 下载Oracle客户端和ODBC驱动程序的安装包。

2. 执行安装包并按照提示进行安装。

3. 设置您的ODBC数据源名称和服务名,以便连接到数据库。

4. 测试连接以确保您能够成功连接到Oracle数据库。

2. 使用OCI库

Oracle提供了一个开发包,用于与Oracle数据库进行通信,称为Oracle Call Interface(OCI)。它是一个用于C/C++的库,具有高级别和低级别API。您可以使用OCI库从C语言应用程序中调用Oracle数据库功能。

下面是连接到Oracle数据库并查询一个表的示例代码:

#include 
#include
void checkerr(errhp, status) /* 处理错误信息*/
OCIError *errhp;
sword status;
{
text errbuf[512];
sb4 errcode = 0;
switch (status)
{
case OCI_SUCCESS:
break;
case OCI_SUCCESS_WITH_INFO:
printf("Error - OCI_SUCCESS_WITH_INFO\n");
break;
case OCI_NEED_DATA:
printf("Error - OCI_NEED_DATA\n");
break;
case OCI_NO_DATA:
printf("Error - OCI_NODATA\n");
break;
case OCI_ERROR:
OCIErrorGet((dvoid *)errhp, (ub4) 1, (text *) NULL, &errcode,
errbuf, (ub4) sizeof(errbuf), OCI_HTYPE_ERROR);
printf("Error - %.*s\n", 512, errbuf);
break;
case OCI_INVALID_HANDLE:
printf("Error - OCI_INVALID_HANDLE\n");
break;
case OCI_STILL_EXECUTING:
printf("Error - OCI_STILL_EXECUTING\n");
break;
case OCI_CONTINUE:
printf("Error - OCI_CONTINUE\n");
break;
default:
break;
}
}

int mn() {
OCIEnv* envhp;
OCIError* errhp;
OCIServer* srvhp;
OCISvcCtx* svchp;
OCIStmt* stmthp;
OCIDefine* defhp;
OCIDescribe* desc;
text* stmt = (text*)"SELECT * FROM EMP";
sword status;
int empno;
text ename[32], job[32];
float sal;

OCIInitialize((ub4)OCI_DEFAULT, (dvoid *)0, (dvoid * (*)(dvoid *,size_t)) 0,
(dvoid * (*)(dvoid *, dvoid *, size_t))0, (void (*)(dvoid *, dvoid *)) 0 );
OCIEnvInit( &envhp, OCI_DEFAULT, 0, 0 );

OCIHandleAlloc( envhp, (dvoid**)&errhp, OCI_HTYPE_ERROR, 0, 0 );

OCIHandleAlloc( envhp, (dvoid**)&srvhp, OCI_HTYPE_SERVER, 0, 0 );

OCIServerAttach( srvhp, errhp, (text *)"ORCL", strlen("ORCL"), OCI_DEFAULT );

OCIHandleAlloc( envhp, (dvoid**)&svchp, OCI_HTYPE_SVCCTX, 0, 0 );

OCIAttrSet( svchp, OCI_HTYPE_SVCCTX, srvhp, 0, OCI_ATTR_SERVER, errhp );

OCIHandleAlloc( envhp, (dvoid**)&stmthp, OCI_HTYPE_STMT, 0, 0 );

OCIStmtPrepare( stmthp, errhp, stmt, strlen((char*)stmt), OCI_NTV_SYNTAX, OCI_DEFAULT );

OCIHandleAlloc( envhp, (dvoid**)&desc, OCI_HTYPE_DESCRIBE, 0, 0 );

OCIDescribeAny( svchp, errhp, stmt, strlen((char*)stmt),
OCI_OTYPE_NAME, OCI_DEFAULT, OCI_PTYPE_UNKOWN, desc );
OCIAttrGet( desc, OCI_HTYPE_DESCRIBE, (dvoid**)&defhp, 0, OCI_ATTR_PARAM, errhp );

OCIDefineByPos( stmthp, &defhp, errhp, 1, (dvoid*)&empno, sizeof(empno), SQLT_INT, 0, 0, 0, OCI_DEFAULT );
OCIDefineByPos( stmthp, &defhp, errhp, 2, (dvoid*)&ename, sizeof(ename), SQLT_STR, 0, 0, 0, OCI_DEFAULT );
OCIDefineByPos( stmthp, &defhp, errhp, 3, (dvoid*)&job, sizeof(job), SQLT_STR, 0, 0, 0, OCI_DEFAULT );
OCIDefineByPos( stmthp, &defhp, errhp, 4, (dvoid*)&sal, sizeof(sal), SQLT_FLT, 0, 0, 0, OCI_DEFAULT );
OCIStmtExecute( svchp, stmthp, errhp, 1, 0, 0, 0, OCI_DEFAULT );

while ((status = OCIStmtFetch2( stmthp, errhp, 1, OCI_FETCH_NEXT, 0, OCI_DEFAULT)) == OCI_SUCCESS)
{
printf("\n Emp No : %d \n", empno);
printf("\n Emp Name : %s \n", ename);
printf("\n Emp Job : %s \n", job);
printf("\n Emp Sal : %.2f \n", sal);
}

OCIHandleFree( envhp, OCI_HTYPE_STMT, (dvoid *)stmthp );
OCIHandleFree( envhp, OCI_HTYPE_DESCRIBE, (dvoid *)desc );
OCIHandleFree( envhp, OCI_HTYPE_SVCCTX, (dvoid *)svchp );
OCIServerDetach( srvhp, errhp, OCI_DEFAULT );
OCIHandleFree( envhp, OCI_HTYPE_SERVER, (dvoid *)srvhp );
OCIHandleFree( envhp, OCI_HTYPE_ERROR, (dvoid *)errhp );
OCIEnvClean( (dvoid **)&envhp, OCI_DEFAULT );
OCITerminate( OCI_DEFAULT );
return 0;
}

3. 使用ODBC API

除了OCI库外,您还可以使用ODBC API来操作Oracle数据库。ODBC API不仅可以与Oracle数据库一起使用,还可以与其他许多数据库系统进行交互,因此它是一种既通用又简便的方法。

下面是连接到Oracle数据库并查询一个表的示例代码:

#include 
#include
#include
#include
#define ERRMSG_LEN 1024

int mn(int argc, char **argv) {
SQLHENV henv; /* Environment handle */
SQLHDBC hdbc; /* Connection handle */
SQLHSTMT hstmt; /* Statement handle */
SQLRETURN retcode; /* Sql return code */
char errmsg[ERRMSG_LEN + 1]; /* Error message buffer */
SQLTCHAR sqlstmt[1024];

retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) {
fprintf(stderr, "Fled to allocate environment handle\n");
exit(1);
}

retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0);
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) {
fprintf(stderr, "Fled to set environment attribute\n");
exit(1);
}

retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) {
fprintf(stderr, "Fled to allocate connection handle\n");
exit(1);
}

retcode = SQLConnect(hdbc, (SQLTCHAR*)"DSN=Oracle;UID=system;PWD=oracle;");
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) {
SQLGetDiagRec(SQL_HANDLE_DBC, hdbc, 1, (SQLTCHAR*)errmsg, NULL, (SQLCHAR*)

数据运维技术 » C语言操作Oracle数据库的开发技巧(c 操作oracle开发)