C语言编写Oracle交易一次体验(c写oracle交易)

C语言编写Oracle交易:一次体验

随着互联网技术的不断发展,大型数据库的应用越来越广泛,例如Oracle数据库,它在企业级应用中具有重要作用。而在开发应用程序时,与Oracle数据库的交互也成为了一种常见的方式。

本文将介绍如何使用C语言编写一个简单的Oracle交易并进行体验。

我们需要准备好开发环境。这里以Windows 10操作系统为例,需要安装以下软件:

1. Oracle Instant Client,这是Oracle提供的针对Windows平台的轻量级客户端程序,可以方便地与Oracle数据库进行通信。

2. Visual Studio 2019,这是一款流行的集成开发环境(IDE),可以方便地进行C语言程序开发。

3. Oracle Data Access Components(ODAC) for Visual Studio,这是Oracle官方提供的用于Visual Studio的数据访问组件。

安装完上述软件后,我们就可以开始编写C语言的Oracle交易了。这里我们以查询员工信息为例。代码如下:

“`c

#include

#include

#include

void checkerr (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 CODE = %d\n”, errcode);

printf (“%.*s\n”, 512, errbuf);

if (status == OCI_SUCCESS)

printf (“Success\n”);

else if (status == OCI_SUCCESS_WITH_INFO)

printf (“Success with Info\n”);

else if (status == OCI_NO_DATA)

printf (“No Data Found\n”);

else if (status == OCI_NEED_DATA)

printf (“Need Data\n”);

else if (status == OCI_ERROR)

printf (“Error\n”);

else if (status == OCI_INVALID_HANDLE)

printf (“Invalid Handle\n”);

else if (status == OCI_STILL_EXECUTING)

printf (“Still Executing\n”);

else if (status == OCI_CONTINUE)

printf (“Continue\n”);

}

int mn (void) {

OCIEnv *envhp;

OCIError *errhp;

OCIServer *srvhp;

OCIUserSession *usrhp;

OCIStmt *stmthp;

OCIDefine *defhp1, *defhp2, *defhp3;

OCISession *authp;

OCISvcCtx *svchp;

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, (size_t) 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);

(void) OCIServerAttach (srvhp, errhp, (text *) “ORCL”, (sb4)

strlen ((char *) “ORCL”), OCI_DEFAULT);

OCIHandleAlloc ((dvoid *) envhp, (dvoid **) &usrhp,

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

(void) OCISessionBegin (svchp, errhp, usrhp, OCI_CRED_RDBMS,

OCI_DEFAULT);

OCIHandleAlloc ((dvoid *) envhp, (dvoid **) &authp,

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

(void) OCIAttrSet ((dvoid *) authp, OCI_HTYPE_SVCCTX,

(dvoid *) srvhp, (ub4) 0, OCI_ATTR_SERVER,

errhp);

(void) OCIAttrSet ((dvoid *) usrhp, OCI_HTYPE_SESSION,

(dvoid *) “scott”, (ub4) strlen ((char *) “scott”),

OCI_ATTR_USERNAME, errhp);

(void) OCIAttrSet ((dvoid *) usrhp, OCI_HTYPE_SESSION,

(dvoid *) “tiger”, (ub4) strlen ((char *) “tiger”),

OCI_ATTR_PASSWORD, errhp);

(void) OCISessionBegin (svchp, errhp, authp, OCI_CRED_RDBMS,

OCI_DEFAULT);

OCIHandleAlloc ((dvoid *) envhp, (dvoid **) &svchp,

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

(void) OCIAttrSet ((dvoid *) svchp, (ub4) OCI_HTYPE_SVCCTX,

(dvoid *) authp, (ub4) 0, (ub4) OCI_ATTR_SESSION,

errhp);

OCIHandleAlloc ((dvoid *) envhp, (dvoid **) &stmthp,

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

(void) OCIStmtPrepare (stmthp, errhp,

(text *) “SELECT * FROM EMP”, (ub4) strlen ((char *) “SELECT * FROM EMP”),

(ub4) OCI_NTV_SYNTAX, (ub4) OCI_DEFAULT);

(void) OCIDefineByPos (stmthp, &defhp1, errhp, 1,

(dvoid *) &empno, sizeof (empno), SQLT_INT,

(dvoid *) 0, (ub2 *) 0, (ub2 *) 0, OCI_DEFAULT);

(void) OCIDefineByPos (stmthp, &defhp2, errhp, 2,

(dvoid *) ename, sizeof (ename), SQLT_STR,

(dvoid *) 0, (ub2 *) 0, (ub2 *) 0, OCI_DEFAULT);

(void) OCIDefineByPos (stmthp, &defhp3, errhp, 3,

(dvoid *) &sal, sizeof (sal), SQLT_FLT,

(dvoid *) 0, (ub2 *) 0, (ub2 *) 0, OCI_DEFAULT);

(void) OCIStmtExecute (authp, stmthp, errhp, (ub4) 0,

(ub4) 0, (CONST OCISnapshot *) NULL, (OCISnapshot *)

NULL, OCI_DEFAULT);

while (OCIStmtFetch (stmthp, errhp, 1, OCI_FETCH_NEXT,

OCI_DEFAULT) == OCI_SUCCESS) {

printf (“Emp %d, Name %s, Salary %f\n”, empno,

ename, sal);

}

OCIHandleFree ((dvoid *) stmthp, OCI_HTYPE_STMT);

OCIHandleFree ((dvoid *) svchp, OCI_HTYPE_SVCCTX);

OCIHandleFree ((dvoid *) authp, OCI_HTYPE_SVCCTX);

OCISessionEnd (usrhp, errhp, authp, OCI_DEFAULT);

OCIServerDetach (srvhp, errhp, OCI_DEFAULT);

OCIHandleFree ((dvoid *) usrhp, OCI_HTYPE_SESSION);

OCIHandleFree ((dvoid *) srvhp, OCI_HTYPE_SERVER);

OCIHandleFree ((dvoid *) errhp, OCI_HTYPE_ERROR);

OCIEnvDispose ((dvoid *) envhp, (dvoid *) 0, (dvoid *) 0);

return 0;

}


在这个程序中,我们使用OCI(Oracle Call Interface)提供的函数库来与Oracle数据库进行交互。在主函数中,我们首先初始化OCI环境,并分配各种必要的句柄,例如srvhp表示连接到Oracle数据库的服务器句柄。

接下来,我们连接到Oracle数据库,并进行用户身份验证。这里我们使用了用户名为scott,密码为tiger的用户进行操作。在进行身份验证之后,我们使用OCIStmtPrepare函数和OCIDefineByPos函数执行了一条简单的查询语句,用来查询员工信息。

在得到查询结果后,我们通过OCIStmtFetch函数将结果输出到控制台上。最后在结束程序之前,我们需要释放分配的所有句柄。

通过以上C语言代码的实现,我们实现了一个简单的Oracle交易,并可通过修改代码来实现其他类型的交易。这种方式的交互方式较

数据运维技术 » C语言编写Oracle交易一次体验(c写oracle交易)