C语言获取Oracle数据库游标(c 获取oracle游标)
C语言获取Oracle数据库游标
Oracle数据库是一种功能强大的关系型数据库,其支持多种编程语言访问数据和获取游标。本文将重点介绍如何使用C语言获取Oracle数据库游标,以帮助开发者更好地操作数据库。
获取Oracle数据库游标的前提条件是需要安装Oracle OCI库,OCI即Oracle Call Interface,是Oracle公司提供的用于C/C++访问Oracle数据库的API。在安装OCI库后,我们需要使用Oracle提供的头文件,包括oci.h、oratypes.h、orl.h等。
以下是C语言获取Oracle数据库游标的基本代码:
“`c
#include
#include
#include “oci.h”
static OCIEnv *envhp;
static OCIServer *srvhp;
static OCISession *authp;
static OCIError *errhp;
static OCISvcCtx *svchp;
int mn(int argc, char **argv)
{
int ret = 0;
OCIStmt *stmt;
OCIParam *param;
OCIParam *param2;
OCIDefine *defnp;
OCIDefine *defnp2;
OCILobLocator *lob;
ub4 num_cols;
ub4 i;
ub4 col_width;
ub1 piece;
ub4 lob_length;
ub1 buf[4096];
ub4 buf_len;
// 初始化OCI环境
ret = OCIEnvCreate(&envhp, OCI_DEFAULT, NULL, NULL, NULL, NULL, 0, NULL);
if (ret != OCI_SUCCESS)
{
printf(“OCIEnvCreate error: %d\n”, ret);
return -1;
}
// 创建OCI错误处理器
ret = OCIHandleAlloc(envhp, (void **) &errhp, OCI_HTYPE_ERROR, 0, NULL);
if (ret != OCI_SUCCESS)
{
printf(“OCIHandleAlloc error: %d\n”, ret);
return -1;
}
// 创建OCI服务器句柄
ret = OCIHandleAlloc(envhp, (void **) &srvhp, OCI_HTYPE_SERVER, 0, NULL);
if (ret != OCI_SUCCESS)
{
printf(“OCIHandleAlloc error: %d\n”, ret);
return -1;
}
// 创建OCI会话句柄
ret = OCIHandleAlloc(envhp, (void **) &authp, OCI_HTYPE_SESSION, 0, NULL);
if (ret != OCI_SUCCESS)
{
printf(“OCIHandleAlloc error: %d\n”, ret);
return -1;
}
// 服务器上执行OCI服务器连接句柄
char *host = “localhost”;
char *user = “root”;
char *passwd = “password”;
char *db = “db1”;
ret = OCIServerAttach(srvhp, errhp, (text *) host, strlen(host), 0);
if (ret != OCI_SUCCESS)
{
printf(“OCIServerAttach error: %d\n”, ret);
return -1;
}
// 设置OCI服务器句柄
ret = OCIAttrSet(authp, OCI_HTYPE_SESSION, (void *) user, strlen(user), OCI_ATTR_USERNAME, errhp);
if (ret != OCI_SUCCESS)
{
printf(“OCIAttrSet error: %d\n”, ret);
return -1;
}
ret = OCIAttrSet(authp, OCI_HTYPE_SESSION, (void *) passwd, strlen(passwd), OCI_ATTR_PASSWORD, errhp);
if (ret != OCI_SUCCESS)
{
printf(“OCIAttrSet error: %d\n”, ret);
return -1;
}
ret = OCISessionBegin(svchp, errhp, authp, OCI_CRED_RDBMS, OCI_DEFAULT);
if (ret != OCI_SUCCESS)
{
printf(“OCISessionBegin error: %d\n”, ret);
return -1;
}
// 获取OCI会话上下文指针
ret = OCIAttrGet(authp, OCI_HTYPE_SESSION, &svchp, 0, OCI_ATTR_SESSION, errhp);
if (ret != OCI_SUCCESS)
{
printf(“OCIAttrGet error: %d\n”, ret);
return -1;
}
// 创建OCI语句句柄
ret = OCIHandleAlloc(envhp, (void **) &stmt, OCI_HTYPE_STMT, 0, NULL);
if (ret != OCI_SUCCESS)
{
printf(“OCIHandleAlloc error: %d\n”, ret);
return -1;
}
char *sql = “SELECT * FROM TABLE1”;
ret = OCIStmtPrepare(stmt, errhp, (text *) sql, strlen(sql), OCI_NTV_SYNTAX, OCI_DEFAULT);
if (ret != OCI_SUCCESS)
{
printf(“OCIStmtPrepare error: %d\n”, ret);
return -1;
}
// 执行OCI语句
ret = OCIStmtExecute(svchp, stmt, errhp, 1, 0, NULL, NULL, OCI_DEFAULT);
if (ret != OCI_SUCCESS)
{
printf(“OCIStmtExecute error: %d\n”, ret);
return -1;
}
// 获取OCI结果列数
ret = OCIAttrGet(stmt, OCI_HTYPE_STMT, &num_cols, 0, OCI_ATTR_PARAM_COUNT, errhp);
if (ret != OCI_SUCCESS)
{
printf(“OCIAttrGet error: %d\n”, ret);
return -1;
}
// 获取OCI结果列属性
for (i = 1; i
{
ret = OCIParamGet(stmt, OCI_HTYPE_STMT, errhp, (void **) ¶m, i);
if (ret != OCI_SUCCESS)
{
printf(“OCIAttrGet error: %d\n”, ret);
return -1;
}
// 判断结果列类型
ub2 data_type;
ret = OCIAttrGet(param, OCI_DTYPE_PARAM, &data_type, 0, OCI_ATTR_DATA_TYPE, errhp);
if (ret != OCI_SUCCESS)
{
printf(“OCIAttrGet error: %d\n”, ret);
return -1;
}
// 根据结果列类型进行处理
switch (data_type)
{
case SQLT_CHR:
ret = OCIAttrGet(param, OCI_DTYPE_PARAM, &col_width, 0, OCI_ATTR_DATA_SIZE, errhp);
if (ret != OCI_SUCCESS)
{
printf(“OCIAttrGet error: %d\n”, ret);
return -1;
}
printf(“Result column %d: VARCHAR2(%d)\n”, i, col_width);
ret = OCIHandleAlloc(envhp, (void **) &defnp, OCI_HTYPE_BIND, 0, NULL);
if (ret != OCI_SUCCESS)
{
printf(“OCIHandleAlloc error: %d\n”, ret);
return -1;
}
ret = OCIDefineByPos(stmt, &defnp, errhp, i, buf, sizeof(buf), SQLT_STR, NULL, NULL, NULL, OCI_FETCH_NEXT);
if (ret != OCI_SUCCESS)
{
printf(“OCIDefineByPos error: %d\n”, ret);
return -1;
}
break;
case SQLT_BLOB:
ret = OCIAttrGet(param, OCI_DTYPE_PARAM, &col_width, 0, OCI_ATTR_DATA_SIZE, errhp);
if (ret != OCI_SUCCESS)
{
printf(“OCIAttrGet error: %d\n”, ret);
return -1;
}
printf(“Result column %d: BLOB(%d)\n”, i, col_width);
ret = OCIHandleAlloc(envhp, (void **) &defnp, OCI_HTYPE_DEFINE, 0, NULL);
if (ret != OCI_SUCCESS)
{
printf(“OCIHandleAlloc error: %d\n”, ret);
return -1;
}
ret = OCIAttrGet(param, OCI_DTYPE_PARAM, &lob_length, 0, OCI_ATTR_DATA_SIZE, errhp);
if (ret != OCI_SUCCESS)
{
printf(“OCIAttrGet error: %d\n”, ret);
return -1;
}
lob = NULL;
ret = OCIDescriptorAlloc(envhp, (void **) &lob, OCI_DTYPE_LOB, 0, NULL);
if (ret != OCI_SUCCESS)
{
printf(“OCIDescriptorAlloc error: %d\n”, ret);
return -1;
}
ret = OCIDefineByPos(stmt, &defnp, errhp, i, &lob, sizeof(OCILobLocator *), SQLT_BLOB, NULL, NULL, NULL, OCI_FETCH_NEXT);
if (ret != OCI_SUCCESS)
{
printf(“OCIDefineByPos error: %d\n”, ret);
return -1;
}
break;
default:
printf(“Result column %d: UNKNOWN TYPE\n”, i);
break;
}
// 获取OCI绑定偏移量
ret = OCIAttrGet(defnp, OCI_HTYPE_DEFINE, &piece, 0, OCI_ATTR_PIECEWISE, errhp);
if (ret != OCI_SUCCESS