C语言连接Oracle教程从入门到精通(c 连接oracle教程)

C语言连接Oracle教程:从入门到精通

在当今数字化时代,数据处理变得愈发重要。而对于企业级应用而言,Oracle数据库是国内外很多企业的必选。而C语言作为被广泛应用的编程语言之一,也需要寻找与Oracle数据库连接的方法。本篇文章将从入门到精通介绍C语言连接Oracle数据库的方法。

一、安装Oracle数据库客户端

在C语言中连接Oracle数据库,需要先安装Oracle数据库客户端。在Oracle官方网站下载到相应的客户端安装包后,按照提示进行安装。安装过程中,请注意选择正确的处理器位数。

二、配置Oracle数据库客户端环境变量

安装完成后,需要配置环境变量。以64位操作系统为例,打开系统环境变量,新建ORACLE_HOME变量并设置其值为安装路径。在Path变量中加入%ORACLE_HOME%\bin。接着,配置TNS_ADMIN环境变量并设置其值为Oracle客户端的network/admin目录路径。

三、连接Oracle数据库

连接Oracle数据库需要调用Oracle提供的API函数。以下是一个连接Oracle数据库的示例:

#include 
#include
#include
#include
int mn(int argc, char ** argv){
OCIEnv *envhp;
OCIServer *srvhp;
OCIError *errhp;
OCISession *usrhp;
OCISvcCtx *svchp;
char *uid = "用户名";
char *pwd = "密码";
char *db = "数据库名";
sword r = OCI_SUCCESS;
r = OCIInitialize(OCI_DEFAULT, NULL, NULL, NULL, NULL);
if (r != OCI_SUCCESS){
return -1;
}
OCIEnvInit(&envhp, OCI_DEFAULT, 0, NULL);
OCIHandleAlloc((dvoid*) envhp, (dvoid**) &errhp, OCI_HTYPE_ERROR, 0, NULL);
OCIHandleAlloc((dvoid*) envhp, (dvoid**) &srvhp, OCI_HTYPE_SERVER, 0, NULL);
OCIServerAttach(srvhp, errhp, (text *) db, strlen(db), OCI_DEFAULT);
OCIHandleAlloc((dvoid*) envhp, (dvoid**) &svchp, OCI_HTYPE_SVCCTX, 0, NULL);
OCIAttrSet((dvoid*) svchp, OCI_HTYPE_SVCCTX, (dvoid*) srvhp, 0, OCI_ATTR_SERVER, errhp);
OCIHandleAlloc((dvoid*) envhp, (dvoid**) &usrhp, OCI_HTYPE_SESSION, 0, NULL);
OCIAttrSet((dvoid*) usrhp, OCI_HTYPE_SESSION, (dvoid*) uid, strlen(uid), OCI_ATTR_USERNAME, errhp);
OCIAttrSet((dvoid*) usrhp, OCI_HTYPE_SESSION, (dvoid*) pwd, strlen(pwd), OCI_ATTR_PASSWORD, errhp);
r = OCISessionBegin(svchp, errhp, usrhp, OCI_CRED_RDBMS, OCI_DEFAULT);
if (r != OCI_SUCCESS){
return -1;
}
OCIHandleAlloc((dvoid*) envhp, (dvoid**)&usrhp, OCI_HTYPE_STMT, 0, NULL);
OCIHandleFree((dvoid*) usrhp, OCI_HTYPE_STMT);
OCIErrorGet(errhp, 1, NULL, &r, (OraText*) "demo error msg", 32, OCI_HTYPE_ERROR);
OCIStmtRelease(usrhp, errhp, NULL, 0, NULL);

OCILogoff(svchp, errhp);
OCIServerDetach(srvhp, errhp, OCI_DEFAULT);
OCIHandleFree((dvoid*) usrhp, OCI_HTYPE_SESSION);
OCIHandleFree((dvoid*) svchp, OCI_HTYPE_SVCCTX);
OCIHandleFree((dvoid*) srvhp, OCI_HTYPE_SERVER);
OCIHandleFree((dvoid*) errhp, OCI_HTYPE_ERROR);
OCIEnvClose(envhp);

return 0;
}

四、执行SQL语句

连接Oracle数据库后,可以执行SQL语句并获取返回结果。以下是一个执行SQL语句的示例:

#include 
#include
#include
#include
#define MAXLEN 256

int mn(int argc, char ** argv){
OCIEnv *envhp;
OCIServer *srvhp;
OCIError *errhp;
OCISession *usrhp;
OCISvcCtx *svchp;
OCIStmt *stmthp = NULL;
OCIDefine *defhp = NULL;
int i = 0;
char *uid = "用户名";
char *pwd = "密码";
char *db = "数据库名";
char strSql[MAXLEN] = "SELECT * FROM TABLENAME";
sword r = OCI_SUCCESS;
OCINumber num;
OCIInitialize(OCI_DEFAULT, NULL, NULL, NULL, NULL);
OCIEnvInit(&envhp, OCI_DEFAULT, 0, NULL);
OCIHandleAlloc((dvoid*) envhp, (dvoid**) &srvhp, OCI_HTYPE_SERVER, 0, NULL);
OCIServerAttach(srvhp, errhp, (text *) db, strlen(db), OCI_DEFAULT);
OCIHandleAlloc((dvoid*) envhp, (dvoid**) &errhp, OCI_HTYPE_ERROR, 0, NULL);
OCIHandleAlloc((dvoid*) envhp, (dvoid**) &svchp, OCI_HTYPE_SVCCTX, 0, NULL);
OCIAttrSet((dvoid*) svchp, OCI_HTYPE_SVCCTX, (dvoid*) srvhp, 0, OCI_ATTR_SERVER, errhp);
OCIHandleAlloc((dvoid*) envhp, (dvoid**) &usrhp, OCI_HTYPE_SESSION, 0, NULL);
OCIAttrSet((dvoid*) usrhp, OCI_HTYPE_SESSION, (dvoid*) uid, strlen(uid), OCI_ATTR_USERNAME, errhp);
OCIAttrSet((dvoid*) usrhp, OCI_HTYPE_SESSION, (dvoid*) pwd, strlen(pwd), OCI_ATTR_PASSWORD, errhp);
r = OCISessionBegin(svchp, errhp, usrhp, OCI_CRED_RDBMS, OCI_DEFAULT);
OCIHandleAlloc((dvoid*) envhp, (dvoid**) &stmthp, OCI_HTYPE_STMT, 0, NULL);
OCIStmtPrepare(stmthp, errhp, (text *) strSql, strlen(strSql), OCI_NTV_SYNTAX, OCI_DEFAULT);
r = OCIStmtExecute(svchp, stmthp, errhp, 0, 0, NULL, NULL, OCI_DEFAULT);
if (r != OCI_SUCCESS){
return -1;
}
for (i=1; i
OCIHandleAlloc((dvoid*) envhp, (dvoid**) &defhp, OCI_HTYPE_DEFINE, 0, NULL);
OCIStmtGetPieceInfo(stmthp, errhp, (dvoid **)&defhp, &i, &doraType, &pInd, &pRCode);
OCIHandleFree((dvoid*) defhp, OCI_HTYPE_DEFINE);
int val = 0;
OCINumber number = {0};
switch(doraType){
case SQLT_INT:
sprintf(data, "%d", val);
printf("%s\n", data);
break;
case SQLT_NUM:
OCIHandleAlloc(envhp, (dvoid**)&defhp, OCI_HTYPE_DEFINE, 0, NULL);
OCIDefineByPos(stmthp, &defhp, errhp, i, &number, sizeof(OCINumber), SQLT_NUM, &pInd, &pRCode, 0, OCI_DEFAULT);
OCINumberToInt(errhp, &number, sizeof(val), OCI_NUMBER_SIGNED, &val);
sprintf(data, "%d", val);
printf("%s\n", data);
OCIHandleFree((dvoid*) defhp, OCI_HTYPE_DEFINE);
break;
case SQLT_STR:
OCIHandleAlloc(envhp, (dvoid**)&defhp, OCI_HTYPE_DEFINE, 0, NULL);
OCIDefineByPos(stmthp, &defhp, errhp, i, data, MAXLEN, SQLT_STR, &pInd, &pRCode, 0, OCI_DEFAULT);
printf("%s\n", data);
OCIHandleFree((dvoid*) defhp, OCI_HTYPE_DEFINE);
break;
default:break;
}
}
OCIStmtRelease(stmthp, errhp, NULL, 0, NULL);
OCISessionEnd(svchp, errhp, usrhp, OCI_DEFAULT);
OCIServerDetach(srvhp, errhp, OCI_DEFAULT);
OCIHandleFree(stmthp, OCI_HTYPE_STMT);
OCIHandleFree(errhp, OCI_HTYPE_ERROR);
OCIHandleFree(svchp, OCI_HTYPE_SVCCTX);
OCIHandle

数据运维技术 » C语言连接Oracle教程从入门到精通(c 连接oracle教程)