C语言连接Oracle数据库实战案例(c 连接oracle案例)

C语言连接Oracle数据库实战案例

Oracle数据库是市场上最受欢迎的关系型数据库之一,它提供了一个强大、灵活和可扩展的平台来处理最关键的企业级应用程序。C语言是一种高效的编程语言,广泛应用于系统编程和嵌入式系统开发。在本文中,我们将介绍如何使用C语言连接Oracle数据库,并提供一个实战案例。

1. 安装Oracle Instant Client

Oracle Instant Client是一种轻量级的客户端,可以快速安装和配置,并可用于连接Oracle数据库。在开始使用C语言连接Oracle数据库之前,我们需要安装Oracle Instant Client。

下载Oracle Instant Client:

https://www.oracle.com/database/technologies/instant-client/downloads.html

将Instant Client的目录添加到系统的PATH路径中。例如,在Windows上,可以在命令行中使用以下命令:

set PATH=C:\instantclient_19_11;%PATH%

2. C语言连接Oracle数据库

C语言可以通过Oracle提供的C API来连接Oracle数据库。这个API提供了很多函数用于连接数据库、执行SQL语句、获取查询结果等。以下是一个简单的C语言程序,使用Oracle C API连接Oracle数据库、执行SQL语句并输出结果:

“`c

#include

#include

#include

int mn()

{

OCIEnv *envhp = NULL;

OCIError *errhp = NULL;

OCIServer *srvhp = NULL;

OCISvcCtx *svchp = NULL;

OCIStmt *stmthp = NULL;

OCIDefine *defhp = NULL;

int ret = 0;

char sql[] = “SELECT * FROM EMP”;

// 初始化OCI环境

OCIEnvCreate(&envhp, OCI_THREADED, NULL, NULL, NULL, NULL, 0, NULL);

OCIHandleAlloc(envhp, (void**)&errhp, OCI_HTYPE_ERROR, 0, NULL);

// 建立服务器连接

OCIHandleAlloc(envhp, (void**)&srvhp, OCI_HTYPE_SERVER, 0, NULL);

OCILogon(envhp, errhp, &svchp, “scott”, strlen(“scott”), “tiger”, strlen(“tiger”), “orcl”, strlen(“orcl”));

// 准备SQL语句

OCIHandleAlloc(envhp, (void**)&stmthp, OCI_HTYPE_STMT, 0, NULL);

OCIStmtPrepare(stmthp, errhp, (text*)sql, strlen(sql), OCI_NTV_SYNTAX, OCI_DEFAULT);

// 执行SQL语句

OCIStmtExecute(svchp, stmthp, errhp, 0, 0, NULL, NULL, OCI_DEFAULT);

// 获取查询结果

int empno;

char ename[20];

float sal;

OCIDefine *def1, *def2, *def3;

OCIHandleAlloc(envhp, (void**)&def1, OCI_HTYPE_DEFINE, 0, NULL);

OCIHandleAlloc(envhp, (void**)&def2, OCI_HTYPE_DEFINE, 0, NULL);

OCIHandleAlloc(envhp, (void**)&def3, OCI_HTYPE_DEFINE, 0, NULL);

OCIDefineByPos(stmthp, &def1, errhp, 1, &empno, sizeof(empno), SQLT_INT, NULL, NULL, NULL, OCI_DEFAULT);

OCIDefineByPos(stmthp, &def2, errhp, 2, ename, sizeof(ename), SQLT_STR, NULL, NULL, NULL, OCI_DEFAULT);

OCIDefineByPos(stmthp, &def3, errhp, 3, &sal, sizeof(sal), SQLT_FLT, NULL, NULL, NULL, OCI_DEFAULT);

while ((ret = OCIStmtFetch(stmthp, errhp, 1, OCI_FETCH_NEXT, OCI_DEFAULT)) != OCI_NO_DATA)

{

printf(“%d\t%s\t%.2f\n”, empno, ename, sal);

}

// 释放OCI资源

OCIHandleFree(srvhp, OCI_HTYPE_SERVER);

OCILogoff(svchp, errhp);

OCIHandleFree(envhp, OCI_HTYPE_ENV);

OCIHandleFree(errhp, OCI_HTYPE_ERROR);

return 0;

}


这个程序首先初始化OCI环境,然后连接Oracle数据库,并准备SQL语句。接着执行SQL语句并获取查询结果。最后释放OCI资源。

3. 实战案例:C语言连接Oracle数据库操作

在实战案例中,我们将使用C语言连接Oracle数据库并进行一些简单的操作。我们将创建一个学生管理系统,可以添加、查询和删除学生记录。

3.1 创建表

在Oracle数据库中创建一个学生表,用于存储学生信息。

```sql
CREATE TABLE student (
id INT PRIMARY KEY,
name VARCHAR2(50) NOT NULL,
age INT NOT NULL,
gender VARCHAR2(10) NOT NULL,
score FLOAT NOT NULL
);

3.2 连接数据库

使用C语言连接Oracle数据库,并返回连接句柄。

“`c

OCIConn *conn = NULL;

const char *user = “scott”;

const char *passwd = “tiger”;

const char *db = “ORCL”;

OCIEnvCreate(&envhp, OCI_THREADED, NULL, NULL, NULL, NULL, 0, NULL);

OCIHandleAlloc(envhp, (void**)&errhp, OCI_HTYPE_ERROR, 0, NULL);

OCIHandleAlloc(envhp, (void**)&srvhp, OCI_HTYPE_SERVER, 0, NULL);

OCIServerAttach(srvhp, errhp, (text*)db, strlen(db), OCI_DEFAULT);

OCIHandleAlloc(envhp, (void**)&svcctxhp, OCI_HTYPE_SVCCTX, 0, NULL);

OCIAttrSet(svcctxhp, OCI_HTYPE_SVCCTX, srvhp, 0, OCI_ATTR_SERVER, errhp);

OCIHandleAlloc(envhp, (void**)&authp, OCI_HTYPE_SESSION, 0, NULL);

OCIAttrSet(authp, OCI_HTYPE_SESSION, (void*)user, strlen(user), OCI_ATTR_USERNAME, errhp);

OCIAttrSet(authp, OCI_HTYPE_SESSION, (void*)passwd, strlen(passwd), OCI_ATTR_PASSWORD, errhp);

OCISessionBegin(svcctxhp, errhp, authp, OCI_CRED_RDBMS, OCI_DEFAULT);

OCIAttrSet(svcctxhp, OCI_HTYPE_SVCCTX, authp, 0, OCI_ATTR_SESSION, errhp);

OCIHandleAlloc(envhp, (void**)&conn, OCI_HTYPE_CONN, 0, NULL);

OCIAttrSet(conn, OCI_HTYPE_CONN, svcctxhp, 0, OCI_ATTR_SVCCTX, errhp);

OCIAttrSet(conn, OCI_HTYPE_CONN, (void*)user, strlen(user), OCI_ATTR_USERNAME, errhp);

OCIAttrSet(conn, OCI_HTYPE_CONN, (void*)passwd, strlen(passwd), OCI_ATTR_PASSWORD, errhp);

OCIStmt *stmthp = NULL;

OCIHandleAlloc(envhp, (void**)&stmthp, OCI_HTYPE_STMT, 0, NULL);


3.3 插入记录

使用C语言向学生表中插入一条记录。

```c
char sql[] = "INSERT INTO student (id, name, age, gender, score) VALUES (:1, :2, :3, :4, :5)";
OCIStmtPrepare(stmthp, errhp, (text*)sql, strlen(sql), OCI_NTV_SYNTAX, OCI_DEFAULT);

int id = 1;
char name[] = "John";
int age = 20;
char gender[] = "male";
float score = 90.5;

OCIBind *bind1, *bind2, *bind3, *bind4, *bind5;
OCIHandleAlloc(envhp, (void**)&bind1, OCI_HTYPE_BIND, 0, NULL);
OCIHandleAlloc(envhp, (void**)&bind2, OCI_HTYPE_BIND, 0, NULL);
OCIHandleAlloc(envhp, (void**)&bind3, OCI_HTYPE_BIND, 0, NULL);
OCIHandleAlloc(envhp, (void**)&bind4, OCI_HTYPE_BIND, 0, NULL);
OCIHandleAlloc(envhp, (void**)&bind5, OCI_HTYPE_BIND, 0, NULL);
OCIBindByPos(stmthp, &bind1, errhp, 1, &id, sizeof(id), SQLT_INT, NULL, NULL, NULL, 0, NULL, OCI_DEFAULT);
OCIBindByPos(stmthp, &bind2, errhp, 2, &name, strlen(name), SQLT_STR, NULL, NULL, NULL, 0, NULL, OCI_DEFAULT);
OCIBindByPos(stmthp, &bind3, errhp, 3, &age, sizeof(age), SQLT_INT, NULL, NULL

数据运维技术 » C语言连接Oracle数据库实战案例(c 连接oracle案例)