Oracle数据库之旅用C语言体验超凡之美(c oracle 库)
概述:
Oracle是可伸缩的关系型数据库,其提供了极为灵活、可靠、高效的数据管理解决方案。与其它数据库系统不同,Oracle数据库设计是基于SQL的标准,可以支持多种平台。本篇文章将介绍用C语言和Oracle交互的方法,来体验Oracle的强大之处。
正文:
1.下载Oracle Instant Client并安装
在Oracle官网上下载Oracle Instant Client,这是一个用于连接Oracle数据库的客户端程序。安装过程就不细说了,后面会用到其中的一些文件。
2.配置环境变量
设置LD_LIBRARY_PATH环境变量,使其指向Instant Client所在目录的lib子目录,以保证程序运行时能找到Instant Client的动态链接库。
export LD_LIBRARY_PATH=/u01/app/oracle/instantclient_12_2/lib
3.编写C程序
1) 包含Oracle数据库头文件
#include
#include
#include
#include
2) 定义Oracle连接变量
OCIEnv *pEnv = NULL;
OCIError *pErr = NULL;
OCISvcCtx *pSvc = NULL;
OCIStmt *pStmt = NULL;
OCIDefine *pDefine = NULL;
OCIDateTime *pNetTime = NULL;
char connString[] = “User Id=SCOTT; Password=TIGER; Data Source=ORCL”;
其中connString是要连接的Oracle数据库的信息,包括用户名、密码和数据源。
3) 初始化Oracle环境
OCIEnvCreate(&pEnv, OCI_DEFAULT, 0, 0, 0, 0, 0, 0);
4) 获取错误句柄
OCIHandleAlloc(pEnv, (void**)&pErr, OCI_HTYPE_ERROR, 0, NULL);
5) 获取数据库连接句柄
OCIHandleAlloc(pEnv, (void**)&pSvc, OCI_HTYPE_SVCCTX, 0, NULL);
OCIAttrSet((void*)pSvc, OCI_HTYPE_SVCCTX, (void*)pEnv, 0, OCI_ATTR_ENV, pErr);
OCILogon(pEnv, pErr, &pSvc, (unsigned char*)connString, strlen(connString), NULL, 0, NULL, OCI_DEFAULT);
6) 准备SQL语句
char *selectSql = “select empno, ename, hiredate from emp where to_char(hiredate,’yyyy-mm’) = ‘1987-12′”;
pStmt = NULL;
OCIHandleAlloc(pEnv, (void**)&pStmt, OCI_HTYPE_STMT, 0, NULL);
int rc = OCIStmtPrepare(pStmt, pErr, (unsigned char*)selectSql, strlen(selectSql), OCI_NTV_SYNTAX, OCI_DEFAULT);
if (rc != OCI_SUCCESS) {
printf(“Prepare statement fled!\n”);
return 1;
}
7) 执行SQL语句
rc = OCIStmtExecute(pSvc, pStmt, pErr, 1, 0, NULL, NULL, OCI_DEFAULT);
if (rc != OCI_SUCCESS && rc != OCI_NO_DATA) {
printf(“Execute statement fled!\n”);
return 1;
}
8) 遍历查询结果
int empno;
char ename[20], hiredate[20];
while (OCIStmtFetch(pStmt, pErr, 1, OCI_FETCH_NEXT, OCI_DEFAULT) == OCI_SUCCESS) {
OCIIntDefineHandle(pStmt, &pDefine, pErr, empno, sizeof(empno), SQLT_INT);
OCIStringDefineHandle(pStmt, &pDefine, pErr, ename, sizeof(ename), SQLT_STR);
OCIDateTimeDefineHandle(pStmt, &pDefine, pErr, &pNetTime, SQLT_TIMESTAMP);
OCIAttrGet(pDefine, OCI_DTYPE_PARAM, &hiredate, 0, OCI_ATTR_DATA_TYPE, pErr);
printf(“%d\t%s\t%s\n”, empno, ename, hiredate);
}
9) 释放资源
OCIHandleFree((void*)pStmt, OCI_HTYPE_STMT);
OCILogoff(pSvc, pErr);
OCIHandleFree((void*)pSvc, OCI_HTYPE_SVCCTX);
OCIHandleFree((void*)pErr, OCI_HTYPE_ERROR);
if (pEnv) OCIHandleFree((void*)pEnv, OCI_HTYPE_ENV);
完整代码:
#include
#include
#include
#include
int mn(int argc, char *argv[])
{
OCIEnv *pEnv = NULL;
OCIError *pErr = NULL;
OCISvcCtx *pSvc = NULL;
OCIStmt *pStmt = NULL;
OCIDefine *pDefine = NULL;
OCIDateTime *pNetTime = NULL;
char connString[] = “User Id=SCOTT; Password=TIGER; Data Source=ORCL”;
char *selectSql = “select empno, ename, hiredate from emp where to_char(hiredate,’yyyy-mm’) = ‘1987-12′”;
OCIEnvCreate(&pEnv, OCI_DEFAULT, 0, 0, 0, 0, 0, 0);
OCIHandleAlloc(pEnv, (void**)&pErr, OCI_HTYPE_ERROR, 0, NULL);
OCIHandleAlloc(pEnv, (void**)&pSvc, OCI_HTYPE_SVCCTX, 0, NULL);
OCIAttrSet((void*)pSvc, OCI_HTYPE_SVCCTX, (void*)pEnv, 0, OCI_ATTR_ENV, pErr);
OCILogon(pEnv, pErr, &pSvc, (unsigned char*)connString, strlen(connString), NULL, 0, NULL, OCI_DEFAULT);
pStmt = NULL;
OCIHandleAlloc(pEnv, (void**)&pStmt, OCI_HTYPE_STMT, 0, NULL);
int rc = OCIStmtPrepare(pStmt, pErr, (unsigned char*)selectSql, strlen(selectSql), OCI_NTV_SYNTAX, OCI_DEFAULT);
if (rc != OCI_SUCCESS) {
printf(“Prepare statement fled!\n”);
return 1;
}
rc = OCIStmtExecute(pSvc, pStmt, pErr, 1, 0, NULL, NULL, OCI_DEFAULT);
if (rc != OCI_SUCCESS && rc != OCI_NO_DATA) {
printf(“Execute statement fled!\n”);
return 1;
}
int empno;
char ename[20], hiredate[20];
while (OCIStmtFetch(pStmt, pErr, 1, OCI_FETCH_NEXT, OCI_DEFAULT) == OCI_SUCCESS) {
OCIIntDefineHandle(pStmt, &pDefine, pErr, empno, sizeof(empno), SQLT_INT);
OCIStringDefineHandle(pStmt, &pDefine, pErr, ename, sizeof(ename), SQLT_STR);
OCIDateTimeDefineHandle(pStmt, &pDefine, pErr, &pNetTime, SQLT_TIMESTAMP);
OCIAttrGet(pDefine, OCI_DTYPE_PARAM, &hiredate, 0, OCI_ATTR_DATA_TYPE, pErr);
printf(“%d\t%s\t%s\n”, empno, ename, hiredate);
}
OCIHandleFree((void*)pStmt, OCI_HTYPE_STMT);
OCILogoff(pSvc, pErr);
OCIHandleFree((void*)pSvc, OCI_HTYPE_SVCCTX);
OCIHandleFree((void*)pErr, OCI_HTYPE_ERROR);
if (pEnv) OCIHandleFree((void*)pEnv, OCI_HTYPE_ENV);
return 0;
}
总结:
C语言作为一种强大的编程语言,在与Oracle数据库连接时也有很大的优势。通过使用OCI库,我们可以方便地在C程序中对Oracle数据库进行操作,使其变得更具灵活性、可定制性和可扩展性。希望大家能够从这篇文章中体会到Oracle数据库的强大之处,也可以用C语言体验一下超凡之美。