c 查询oracle

2023年 8月 4日 68.4k 0

对于使用C语言进行开发的程序员来说,在处理数据库相关的数据时,Oracle数据库是一个比较常见的选择。Oracle数据库是当前世界上最大的关系数据库管理系统,它可以提供很多有用的功能来支持开发人员进行数据管理和处理。

在C语言中,要实现与Oracle数据库的交互,一般是通过ODBC或OCI来实现。ODBC是一种开放式数据库连接,它可以访问任何符合ODBC标准的数据库,而OCI是Oracle提供的一种API,它可以直接访问Oracle数据库。

下面我们来看一些具体的案例,来了解如何使用C语言来查询Oracle数据库。

/* 连接Oracle数据库 */
#include
#include
#include
int main()
{
OCIEnv *env;
OCIError *err;
OCISvcCtx *svc;
OCIServer *srv;
if (OCIEnvCreate(&env, OCI_DEFAULT, NULL, NULL, NULL, NULL, 0, NULL))
{
printf("Create Environment Failed\n");
exit(-1);
}
if (OCIHandleAlloc(env, (void **)&err, OCI_HTYPE_ERROR, 0, NULL))
{
printf("Allocate Error Handle Failed\n");
exit(-1);
}
if (OCIHandleAlloc(env, (void **)&srv, OCI_HTYPE_SERVER, 0, NULL))
{
printf("Allocate Server Handle Failed\n");
exit(-1);
}
if (OCIHandleAlloc(env, (void **)&svc, OCI_HTYPE_SVCCTX, 0, NULL))
{
printf("Allocate Service Context Handle Failed\n");
exit(-1);
}
if (OCILogon(env, err, &svc, (OraText *)"test", strlen("test"), (OraText *)"test", strlen("test"), (OraText *)"test", strlen("test")))
{
printf("Logon Oracle Database Failed\n");
exit(-1);
}
printf("Connect Oracle Database Successfully\n");
OCILogoff(svc, err);
OCIHandleFree(svc, OCI_HTYPE_SVCCTX);
OCIHandleFree(srv, OCI_HTYPE_SERVER);
OCIHandleFree(err, OCI_HTYPE_ERROR);
OCIHandleFree(env, OCI_HTYPE_ENV);
return 0;
}

上面的示例代码中,我们使用OCI库连接了一个名为test的Oracle数据库,通过OCILogon函数来实现用户的登录。在实际的开发中,我们可以根据需要,将这个数据库登录的过程封装成一个函数,使其更加易用。

接下来,我们看一下如何使用OCI库来查询Oracle数据库中的数据:

/* 查询Oracle数据库的数据 */
#include
#include
#include
int main()
{
OCIEnv *env;
OCIError *err;
OCISvcCtx *svc;
OCIServer *srv;
OCIDefine *dfn = NULL;
OCIStmt *stmt;
OCIResult *res;
char sql[1024];
sword status;
int id, age;
char name[256];
if (OCIEnvCreate(&env, OCI_DEFAULT, NULL, NULL, NULL, NULL, 0, NULL))
{
printf("Create Environment Failed\n");
exit(-1);
}
if (OCIHandleAlloc(env, (void **)&err, OCI_HTYPE_ERROR, 0, NULL))
{
printf("Allocate Error Handle Failed\n");
exit(-1);
}
if (OCIHandleAlloc(env, (void **)&srv, OCI_HTYPE_SERVER, 0, NULL))
{
printf("Allocate Server Handle Failed\n");
exit(-1);
}
if (OCIHandleAlloc(env, (void **)&svc, OCI_HTYPE_SVCCTX, 0, NULL))
{
printf("Allocate Service Context Handle Failed\n");
exit(-1);
}
if (OCILogon(env, err, &svc, (OraText *)"test", strlen("test"), (OraText *)"test", strlen("test"), (OraText *)"test", strlen("test")))
{
printf("Logon Oracle Database Failed\n");
exit(-1);
}
printf("Connect Oracle Database Successfully\n");
sprintf(sql, "SELECT * FROM test_table WHERE id = 1");
if (OCIHandleAlloc(env, (void **)&stmt, OCI_HTYPE_STMT, 0, NULL))
{
printf("Allocate Statement Handle Failed\n");
exit(-1);
}
if (OCIStmtPrepare(stmt, err, (OraText *)sql, strlen(sql), OCI_NTV_SYNTAX, OCI_DEFAULT))
{
printf("Prepare SQL Statement Failed\n");
exit(-1);
}
if (OCIStmtExecute(svc, stmt, err, 1, 0, NULL, NULL, OCI_DEFAULT))
{
printf("Execute SQL Statement Failed\n");
exit(-1);
}
if (OCIStmtFetch(stmt, err, 1, OCI_FETCH_NEXT, OCI_DEFAULT))
{
printf("Fetch Data Failed\n");
exit(-1);
}
OCIDefineByPos(stmt, &dfn, err, 1, &id, sizeof(id), SQLT_INT, NULL, NULL, NULL, OCI_DEFAULT);
OCIDefineByPos(stmt, &dfn, err, 2, &name, sizeof(name), SQLT_STR, NULL, NULL, NULL, OCI_DEFAULT);
OCIDefineByPos(stmt, &dfn, err, 3, &age, sizeof(age), SQLT_INT, NULL, NULL, NULL, OCI_DEFAULT);
do
{
status = OCIStmtFetch2(stmt, err, 1, OCI_FETCH_NEXT, OCI_DEFAULT, OCI_DEFAULT);
if (status == OCI_SUCCESS)
{
printf("ID: %d, Name: %s, Age: %d\n", id, name, age);
}
else if (status == OCI_NO_DATA)
{
break;
}
else if (status)
{
printf("Fetch Data Error: %d\n", status);
exit(-1);
}
} while (1);
OCIHandleFree(stmt, OCI_HTYPE_STMT);
OCILogoff(svc, err);
OCIHandleFree(svc, OCI_HTYPE_SVCCTX);
OCIHandleFree(srv, OCI_HTYPE_SERVER);
OCIHandleFree(err, OCI_HTYPE_ERROR);
OCIHandleFree(env, OCI_HTYPE_ENV);
return 0;
}

上面的代码实现了一个从名为test_table的数据表中查询数据的过程。首先,我们使用OCIStmtPrepare函数来准备SQL语句;然后使用OCIStmtExecute函数来执行SQL语句;最后,我们通过OCIStmtFetch函数来获取查询结果。

在获取查询结果之后,我们使用OCIDefineByPos函数来定义查询结果的类型,并通过OCIStmtFetch2函数来获取查询结果的数据。

上面给出的代码只是一个比较简单的查询Oracle数据库的例子,开发人员可以根据实际情况,进行更加复杂的操作来满足自己的需求。

相关文章

Oracle如何使用授予和撤销权限的语法和示例
Awesome Project: 探索 MatrixOrigin 云原生分布式数据库
下载丨66页PDF,云和恩墨技术通讯(2024年7月刊)
社区版oceanbase安装
Oracle 导出CSV工具-sqluldr2
ETL数据集成丨快速将MySQL数据迁移至Doris数据库

发布评论