c 操作 oracle

C语言是一种常用的编程语言,被广泛应用于各个领域。而Oracle是世界上最为流行的关系型数据库,也是各个企业中最为常见的数据库之一。在使用C语言开发项目的过程中,经常需要操作Oracle数据库,以完成对数据的增删改查等操作。

在C语言中操作Oracle,需要使用Oracle提供的OCI(Oracle Call Interface)库,这个库提供了一系列的函数,用于与Oracle数据库进行连接、查询、记录集处理等操作。接下来我们来举几个例子来说明在使用C语言操作Oracle数据库时需要用到的一些函数。

//连接Oracle数据库 if ( OCIEnvCreate(&envhp, OCI_DEFAULT, (dvoid *)0, 0, 0, 0, 0, 0) ){ printf( "Error - OCIEnvCreate()\n" ); return -1; } if ( OCIHandleAlloc( (dvoid *)envhp, (dvoid **)&errhp, OCI_HTYPE_ERROR, 0, 0) ){ printf( "Error - OCIHandleAlloc()\n" ); return -1; } if ( OCILogon2(envhp, errhp, &svchp, (OraText*)username, strlen(username), (OraText*)password, strlen(password), (OraText*)database, strlen(database), OCI_DEFAULT) ){ printf( "Error - OCILogon2()\n" ); return -1; } //查询数据 char strSql[512]; sprintf(strSql, "select * from user where name='pancb'"); if ( OCIStmtPrepare(stmthp, errhp, (text*)strSql, strlen(strSql), OCI_NTV_SYNTAX, OCI_DEFAULT) ){ printf( "Error - OCIStmtPrepare()\n" ); return -1; } if ( OCIStmtExecute(svchp, stmthp, errhp, 0, 0, 0, 0, OCI_DEFAULT) ){ printf( "Error - OCIStmtExecute()\n" ); return -1; } //处理记录集 while (OCIStmtFetch2(stmthp, errhp, 1, OCI_FETCH_NEXT, 0, OCI_DEFAULT) != OCI_NO_DATA) { OCIAttrGet(stmthp, OCI_HTYPE_STMT, &nrows, 0, OCI_ATTR_ROW_COUNT, errhp); printf("nrows=%d\n", nrows); //输出结果集有多少条记录 } //释放连接 if (OCILogoff(svchp, errhp)) { printf( "Error - OCILogoff()\n" ); return -1; }