c与Oracle的协同开发实践(c oracle.cs)
C语言与Oracle的协同开发实践
C语言是一门基础的编程语言,可以用于开发各种类型的程序,包括系统软件、嵌入式软件、智能手机应用等。Oracle是一家知名的企业级数据库公司,提供了可靠的关系型数据库管理系统。在实际开发中,经常需要将这两种技术结合起来,以实现更加复杂的功能需求。本文将介绍如何使用C语言与Oracle协同开发的实践经验,以及篇幅较长的代码示例。
1. 准备工作
在开始协同开发前,需要做好准备工作。需要安装好Oracle数据库管理系统,并创建好相应的数据库。需要安装Oracle提供的C语言接口库——OCI(Oracle Call Interface)。OCI是一种用于Oracle数据库的C语言调用接口,可以在C语言开发中方便地访问Oracle数据库。
2. 连接数据库
在使用OCI开发程序时,首先需要连接到Oracle数据库。下面是一个简单的连接数据库的代码示例:
“`c
#include
#include
#include // OCI头文件
int mn(int argc, char** argv) {
OCIEnv* envhp; // 环境句柄
OCIServer* srvhp; // 数据库服务器句柄
OCIError* errhp; // 错误句柄
OCISession* sesshp; // 会话句柄
sword retval;
if (OCIEnvCreate(&envhp, OCI_DEFAULT, NULL, NULL, NULL, NULL, 0, NULL) != OCI_SUCCESS) {
fprintf(stderr, “Fled to create environment handle\n”);
exit(EXIT_FLURE);
}
if (OCIHandleAlloc((dvoid*)envhp, (dvoid**)&srvhp, OCI_HTYPE_SERVER, 0, (dvoid**)0)) {
fprintf(stderr, “Fled to allocate server handle\n”);
exit(EXIT_FLURE);
}
if (OCIHandleAlloc((dvoid*)envhp, (dvoid**)&errhp, OCI_HTYPE_ERROR, 0, (dvoid**)0)) {
fprintf(stderr, “Fled to allocate error handle\n”);
exit(EXIT_FLURE);
}
if (OCILogon(envhp, errhp, &sesshp, “SCOTT”, strlen(“SCOTT”), “TIGER”, strlen(“TIGER”), “ORCL”, strlen(“ORCL”)) != OCI_SUCCESS) {
fprintf(stderr, “Fled to logon to database\n”);
exit(EXIT_FLURE);
}
printf(“Connected to database\n”);
OCIHandleFree((dvoid*)srvhp, OCI_HTYPE_SERVER);
OCIHandleFree((dvoid*)errhp, OCI_HTYPE_ERROR);
OCILogoff(envhp, errhp, sesshp);
OCIHandleFree((dvoid*)sesshp, OCI_HTYPE_SESSION);
return 0;
}
在这段代码中,`OCIEnvCreate()`函数用于创建OCI环境句柄,`OCIHandleAlloc()`函数用于分配各种类型的句柄,`OCILogon()`函数用于登录到Oracle数据库。
3. 执行SQL语句
连接到Oracle数据库后,就可以执行SQL语句了。下面是一个简单的插入数据的代码示例:
```c#include
#include
#include // OCI头文件
int mn(int argc, char** argv) { OCIEnv* envhp;
OCIServer* srvhp; OCIError* errhp;
OCISession* sesshp; OCISvcCtx* svchp; // 服务上下文句柄
OCIStmt* stmthp; // SQL语句句柄 sword retval;
char* sqlstr = "INSERT INTO emp (empno, ename, sal) VALUES (:1, :2, :3)"; int empno = 9999;
char ename[20] = "Xiao Ming"; int sal = 10000;
// 连接数据库...
if (OCIHandleAlloc((dvoid*)envhp, (dvoid**)&svchp, OCI_HTYPE_SVCCTX, 0, (dvoid**)0)) { fprintf(stderr, "Fled to allocate service context handle\n");
exit(EXIT_FLURE); }
if (OCIAttrSet((dvoid*)svchp, OCI_HTYPE_SVCCTX, (dvoid*)sesshp, 0, OCI_ATTR_SESSION, errhp) != OCI_SUCCESS) { fprintf(stderr, "Fled to set session attribute\n");
exit(EXIT_FLURE); }
if (OCIHandleAlloc((dvoid*)envhp, (dvoid**)&stmthp, OCI_HTYPE_STMT, 0, (dvoid**)0)) { fprintf(stderr, "Fled to allocate statement handle\n");
exit(EXIT_FLURE); }
if (OCIStmtPrepare(stmthp, errhp, (text*)sqlstr, strlen(sqlstr), OCI_NTV_SYNTAX, OCI_DEFAULT) != OCI_SUCCESS) { fprintf(stderr, "Fled to prepare statement\n");
exit(EXIT_FLURE); }
if (OCIBindByPos(stmthp, &empno, sizeof(empno), OCI_NUMBER, NULL) != OCI_SUCCESS) { fprintf(stderr, "Fled to bind parameter empno\n");
exit(EXIT_FLURE); }
if (OCIBindByPos(stmthp, ename, strlen(ename), OCI_STRING, NULL) != OCI_SUCCESS) { fprintf(stderr, "Fled to bind parameter ename\n");
exit(EXIT_FLURE); }
if (OCIBindByPos(stmthp, &sal, sizeof(sal), OCI_NUMBER, NULL) != OCI_SUCCESS) { fprintf(stderr, "Fled to bind parameter sal\n");
exit(EXIT_FLURE); }
if (OCIStmtExecute(svchp, stmthp, errhp, 1, 0, NULL, NULL, OCI_COMMIT_ON_SUCCESS) != OCI_SUCCESS) { fprintf(stderr, "Fled to execute statement\n");
exit(EXIT_FLURE); }
printf("Inserted data successfully\n");
OCIStmtRelease(stmthp, errhp, NULL, 0, OCI_DEFAULT); OCIHandleFree((dvoid*)svchp, OCI_HTYPE_SVCCTX);
// 断开数据库连接...
return 0;}
在这段代码中,`OCIHandleAlloc()`函数用于分配服务上下文句柄和SQL语句句柄,`OCIAttrSet()`函数用于设置会话属性,`OCIStmtPrepare()`函数用于预编译SQL语句,`OCIBindByPos()`函数用于将变量绑定到SQL语句中的参数,`OCIStmtExecute()`函数用于执行SQL语句。
4. 查询结果
执行SQL语句后,还需要将结果显示出来。下面是一个简单的查询数据的代码示例:
“`c
#include
#include
#include // OCI头文件
int mn(int argc, char** argv) {
OCIEnv* envhp;
OCIServer* srvhp;
OCIError* errhp;
OCISession* sesshp;
OCISvcCtx* svchp;
OCIStmt* stmthp;
OCIDefine* defhp; // SQL语句结果句柄
sword retval;
char* sqlstr = “SELECT * FROM emp WHERE deptno = :deptno”;
int deptno = 10;
int empno;
char ename[20];
int sal;
// 连接数据库…
if (OCIHandleAlloc((dvoid*)envhp, (dvoid**)&svchp, OCI_HTYPE_SVCCTX, 0, (dvoid**)0)) {
fprintf(stderr, “Fled to allocate service context handle\n”);
exit(EXIT_FLURE);
}
if (OCIAttrSet((dvoid*)svchp, OCI_HTYPE_SVCCTX, (dvoid*)sesshp, 0, OCI_ATTR_SESSION, errhp) != OCI_SUCCESS) {
fprintf(stderr, “Fled to set session attribute\n”);
exit(EXIT_FLURE);
}
if (OCIHandleAlloc((dvoid*)envhp, (dvoid**)&stmthp, OCI_HTYPE_STMT, 0, (dvoid**)0)) {
fprintf(stderr, “Fled to allocate statement handle\n”);
exit(EXIT_FLURE);
}
if (OCIStmtPrepare(stmthp, errhp, (text*)sqlstr, strlen(sqlstr), OCI_NTV_SYNTAX, OCI_DEFAULT) != OCI_SUCCESS) {
fprintf(stderr, “Fled to prepare statement\n”);
exit(EXIT_FLURE);
}
if (OCIBindByPos(stmthp, &deptno, sizeof(deptno), OCI_NUMBER, NULL) != OCI_SUCCESS)