C语言连接Oracle数据库的实现方法(c 中使用oracle)

C语言连接Oracle数据库的实现方法

Oracle是一种典型的关系型数据库,广泛用于企业级应用中。而C语言作为一种高效且广泛应用的编程语言,对于连接Oracle数据库也有一系列的实现方法。

使用Oracle Instant Client 链接库

最简单而又高效的方法是通过Oracle Instant Client链接库来实现连接Oracle数据库。链接库是一种预编译好的二进制文件,可让C语言程序调用其中的函数而无需了解内部细节。

以下是一种连接Oracle数据库的基本代码示例:

“`c

#include

#include

int mn()

{

/*申明变量和初始化过程*/

OCIEnv *p_env ;

OCIError *p_err ;

OCISvcCtx *p_svc ;

OCIStmt *p_stmt;

OCIDefine *p_dfn ;

OCIBind *p_bnd ;

text *szSql = “SELECT col1,col2,col3 FROM TABLE_NAME WHERE col4 =: 1”;

text szUserName[21] = “username” ;

text szPassword[21] = “password” ;

text szDbName[13] = “localhost” ;

text s_logon[50] = { 0 };

sword nType = OCI_DEFAULT;

sb4 n_val;

sword n_cols,i;

sword n_err_code = 0;

sb2 col1[256] ={0};

sb2 col2[256] ={0};

sb2 col3[256] ={0};

sb4 col4;

long nLob ;

OCILobLocator *p_lob ;

/*初始化Oracle链接*/

sprintf ( s_logon,”(DESCRIPTION =(ADDRESS_LIST =(ADDRESS =(PROTOCOL = TCP)(HOST =%s)(PORT =%d)))(CONNECT_DATA =(SID=%s)))”, szDbName, 1521, “oraclesid”) ;

OCIEnvCreate(&p_env, OCI_THREADED|OCI_OBJECT, 0, 0, 0, 0, 0, NULL ) ;

OCIHandleAlloc ((dvoid *) p_env, (dvoid **) &p_err, OCI_HTYPE_ERROR, 0, 0 ) ;

n_err_code = OCILogon(p_env, p_err, &p_svc, szUserName, strlen(szUserName), szPassword, strlen(szPassword), s_logon, strlen(s_logon)) ;

if (n_err_code)

{

printf(“Error: OCILogon(), err= %d\n”, n_err_code) ;

goto error;

}

/*执行SQL语句*/

OCIHandleAlloc((dvoid *)p_env, (dvoid **) &p_stmt, OCI_HTYPE_STMT, 0, 0);

n_err_code = OCIStmtPrepare(p_stmt, p_err, szSql, strlen(szSql), nType, nType);

if (n_err_code != 0)

{

printf(“Error: OCIStmtPrepare(), err= %d\n”, n_err_code);

goto error;

}

n_err_code = OCIStmtBindByName(p_stmt, &p_bnd, p_err, (text *) “:1”, 2, &col4, sizeof(col4), SQLT_INT, NULL, NULL, NULL, 0, NULL, OCI_DEFAULT);

if (n_err_code != 0)

{

printf(“Error: OCIStmtBindByName(), err= %d\n”, n_err_code);

goto error;

}

n_err_code = OCIStmtExecute(p_svc, p_stmt, p_err, 0, 0, NULL, NULL, nType) ;

if (n_err_code != 0)

{

printf(“Error: OCIStmtExecute(), err= %d\n”, n_err_code) ;

goto error;

}

/*处理结果*/

n_err_code = OCIStmtFetch(p_stmt, p_err, n_cols, nType, OCI_DEFAULT) ;

if (n_err_code != OCI_NO_DATA)

{

printf(“Error: OCIStmtFetch(), err= %d\n”, n_err_code) ;

goto error;

}

printf(“%s,%s,%s”,col1,col2,col3);

error:

OCILogoff(p_svc, p_err) ;

OCIHandleFree(p_err, OCI_HTYPE_ERROR) ;

OCIHandleFree(p_stmt, OCI_HTYPE_STMT) ;

OCIHandleFree(p_svc, OCI_HTYPE_SVCCTX) ;

OCIHandleFree(p_env, OCI_HTYPE_ENV);

return 0;

}


在上面的代码中,通过OCIEnvCreate函数初始化Oracle链接,该函数将在C中创建一个环境变量。使用OCILogon函数来建立链接,从而可以访问Oracle数据库。另外,可以使用OCIStmtPrepare函数来准备SQL语句并对其进行参数绑定,以在数据库中执行查询。

OCI接口

OCI(Oracle Call Interface)是Oracle提供的标准C语言接口。与使用Oracle Instant Client的方法类似,使用OCI接口可以方便地实现C与Oracle数据库的连接。通过OCI接口,可以在C语言中实现各种数据库操作,例如读取数据和进行事务。

以下是一种使用OCI接口的Oracle数据库连接代码示例:

```c
#include
#include
#include
int mn()
{
OCIEnv *envhp;
OCIError *errhp;
OCIServer *srvhp;
OCISvcCtx *svchp;
OCIStmt *stmthp;
OCIDefine *defhp;
text *username = (const text *)"username";
text *password = (const text *)"password";
text *dbname = (const text *)"localhost";
text sqlstmt[256];
sword status;
sb2 col1[256] ={0};
sb2 col2[256] ={0};
sb4 col3;
sprintf(sqlstmt, "SELECT col1,col2,col3 FROM TABLE_NAME WHERE col4 =: 1");
/*初始化Oracle链接*/
OCIInitialize((OCI_ENV_DEFAULT), 0, NULL, NULL, (dvoid *)&envhp);
OCIHandleAlloc((dvoid *)envhp, (dvoid **)&errhp, (ub4)OCI_HTYPE_ERROR, (size_t)0, (dvoid **)0);
OCIHandleAlloc((dvoid *)envhp, (dvoid **)&srvhp, (ub4)OCI_HTYPE_SERVER, (size_t)0, (dvoid **)0);
status = OCIServerAttach(srvhp, errhp, (text *)dbname, strlen((const char *)dbname), (ub4)OCI_DEFAULT);
OCIHandleAlloc((dvoid *)envhp, (dvoid **)&svchp, (ub4)OCI_HTYPE_SVCCTX, (size_t)0, (dvoid **)0);
OCIAttrSet((dvoid *)svchp, (ub4)OCI_HTYPE_SVCCTX, (dvoid *)srvhp, (ub4)0, (ub4)OCI_ATTR_SERVER, (OCIError *)errhp);
status = OCIHandleAlloc((dvoid *)envhp, (dvoid **)&stmthp, (ub4)OCI_HTYPE_STMT, (size_t)0, (dvoid **)0);
OCILogon(envhp, errhp, &svchp, username, strlen((const char *)username), password, strlen((const char *)password), dbname, strlen((const char *)dbname));
OCIParse(stmthp, errhp, sqlstmt, strlen(sqlstmt), OCI NTV_SYNTAX, OCI_DEFAULT);
status = OCIBindByName(stmthp, &defhp, errhp, (text *)":1", strlen(":1"), &col3, sizeof(col3), SQLT_INT, (dvoid *)0, (ub2 *)0, (ub2 *)0, 0, (ub4 *)0, OCI_DEFAULT);
OCIStmtExecute(svchp, stmthp, errhp, (ub4)1, (ub4)0, (OCISnapshot *)NULL, (OCISnapshot *)NULL, OCI_DEFAULT);
OCIDefineByPos(stmthp, &defhp, errhp, 1, col1, 256, SQLT_STR, (dvoid *)0, (ub2 *)0, (ub2 *)0, OCI_DEFAULT);
OCIDefineByPos(stmthp, &defhp, errhp, 2, col2, 256, SQLT_STR, (dvoid *)0, (ub2 *)0, (ub2 *)0, OCI_DEFAULT);
OCIDefineByPos(stmthp, &defhp, errhp, 3, &col3, sizeof(col3), SQLT_INT, (dvoid *)0, (ub2 *)0, (ub2 *)0, OCI_DEFAULT);
OCIStmtExecute(svchp, stm

数据运维技术 » C语言连接Oracle数据库的实现方法(c 中使用oracle)