C语言快速操作Oracle数据库(c 直接访问oracle)

C语言快速操作Oracle数据库

Oracle是目前全球最大的企业级数据库管理系统之一,也是众多企业的首选数据库系统。C语言作为一种高效、稳定的编程语言,也被广泛应用于数据处理和数据库操作领域。本文将介绍如何使用C语言快速操作Oracle数据库。

1.安装Oracle客户端

在开始操作Oracle数据库之前,我们需要先安装Oracle客户端软件。Oracle官网提供了Oracle Instant Client的下载,我们可以根据自己的需要选择下载对应版本的客户端。

2.配置Oracle客户端环境变量

安装完成后,我们需要将Oracle客户端的路径添加到系统环境变量中,以便在C语言程序中调用Oracle客户端。在Windows系统中,可以通过以下步骤进行配置:

1)打开“控制面板”,点击“系统和安全”,然后点击“系统”。

2)在左侧导航栏中,点击“高级系统设置”。

3)在“高级”选项卡下,点击“环境变量”。

4)在用户变量或系统变量中,找到名为“Path”的变量,点击“编辑”。在变量值的末尾输入Oracle客户端路径,以分号(;)分隔不同的路径。

3.连接Oracle数据库

在C语言中,我们可以使用Oracle提供的OCI(Oracle Call Interface)或ODBC(Open Database Connectivity)库来进行数据库操作。这里我们以OCI库为例,介绍如何连接Oracle数据库。代码实现如下:

#include

#include

#include

#define DB_USER “username”

#define DB_PASS “password”

#define DB_CONNECT “connstr”

void checkerr(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_NO_DATA\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;

OCISvcCtx *svchp;

OCIStmt *stmthp;

OCIDefine *defhp;

OCIParam *param;

OCIDate *date;

OCIIter *iter;

OCILobLocator *lob;

sword status;

OCIInitialize((ub4)OCI_DEFAULT, (dvoid *)0, (dvoid * (*)(dvoid *, size_t)) 0,

(dvoid * (*)(dvoid *, dvoid *, size_t))0, (void (*)(dvoid *, dvoid *))0 );

OCIEnvInit((OCIEnv **)&envhp, OCI_DEFAULT, 0, (dvoid **)&ctx);

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

OCIHandleAlloc((dvoid *)envhp, (dvoid **)&svcctx, OCI_HTYPE_SVCCTX, (size_t)0, (dvoid **)0);

status = OCILogon(envhp, errhp, &svchp, DB_USER, strlen(DB_USER),

DB_PASS, strlen(DB_PASS), DB_CONNECT, strlen(DB_CONNECT));

checkerr(errhp, status);

printf(“Connected to Oracle.\n”);

OCILogoff(svchp, envhp, errhp);

return 0;

}

在代码中,我们定义了各种OCI库所需的数据结构,通过OCIInitialize()函数初始化OCI环境,并通过OCILogon()函数连接Oracle数据库。其中,第一个参数envhp是OCI环境句柄,第二个参数errhp是错误句柄,第三个参数svchp是服务上下文句柄,第四到六个参数分别是数据库用户名、密码和连接字符串。

4.执行SQL语句

连接成功后,我们就可以执行SQL语句进行数据库操作了。以下是一个简单的例子,用于查询Oracle数据库中的数据:

#include

#include

#include

#define DB_USER “username”

#define DB_PASS “password”

#define DB_CONNECT “connstr”

void checkerr(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_NO_DATA\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;

OCISvcCtx *svchp;

OCIStmt *stmthp;

OCIDefine *defhp;

OCIParam *param;

OCIDate *date;

OCIIter *iter;

OCILobLocator *lob;

sword status;

char *sql = “SELECT * FROM emp”;

ub4 sql_len = strlen(sql);

OCIInitialize((ub4)OCI_DEFAULT, (dvoid *)0, (dvoid * (*)(dvoid *, size_t)) 0,

(dvoid * (*)(dvoid *, dvoid *, size_t))0, (void (*)(dvoid *, dvoid *))0 );

OCIEnvInit((OCIEnv **)&envhp, OCI_DEFAULT, 0, (dvoid **)&ctx);

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

OCIHandleAlloc((dvoid *)envhp, (dvoid **)&svcctx, OCI_HTYPE_SVCCTX, (size_t)0, (dvoid **)0);

status = OCILogon(envhp, errhp, &svchp, DB_USER, strlen(DB_USER),

DB_PASS, strlen(DB_PASS), DB_CONNECT, strlen(DB_CONNECT));

checkerr(errhp, status);

printf(“Connected to Oracle.\n”);

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

status = OCIStmtPrepare(stmthp, errhp, (text *)sql, sql_len, (ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT);

checkerr(errhp, status);

status = OCIStmtExecute(svchp, stmthp, errhp, (ub4)0, (ub4)0, (CONST OCISnapshot *) NULL,

(OCISnapshot *) NULL, OCI_COMMIT_ON_SUCCESS);

checkerr(errhp, status);

status = OCIStmtFetch(stmthp, errhp, (ub4)1, OCI_FETCH_NEXT, OCI_DEFAULT);

checkerr(errhp, status);

do

{

char ename[20], job[9];

int empno;

float sal;

checkerr(errhp, OCIStmtGetNextResult(stmthp, envhp, (dvoid **)&param, (OCIStmt **) 0));

status = OCIParamGet(param, OCI_HTYPE_PARAM, errhp, (dvoid **)&defhp, (ub4)1);

checkerr(errhp, status);

status = OCIDefineByPos(stmthp, &defhp, errhp, 1, (dvoid *)&empno,

(sword)sizeof(empno), SQLT_INT, (dvoid *)0, (ub


数据运维技术 » C语言快速操作Oracle数据库(c 直接访问oracle)