C 语言驱动 Oracle 数据库(c 执行oracle)
C语言驱动Oracle数据库
Oracle是一种由Oracle公司开发的非常流行的关系型数据库管理系统,许多大型机构使用数据库来存储和管理数据。C语言是一种功能强大的编程语言,可以与许多软件系统进行交互,包括数据库。本文将介绍如何使用C语言来驱动Oracle数据库。
一、安装Oracle Instant Client
要与Oracle数据库进行交互,需要在计算机上安装Oracle Instant Client,它是一种轻量级版本的Oracle数据库客户端。通过以下步骤安装:
1.访问Oracle官网,下载Compatible Instant Client版本。
2.解压缩安装文件到任意文件夹,设置环境变量ORACLE_HOME和LD_LIBRARY_PATH。
3.下载和安装ODBC Driver和ODBC Driver Manager。
二、创建Oracle数据库连接
需要连接Oracle数据库,使用用户名和密码连接数据库,并为连接设置其他参数。要创建连接,请使用下列函数:
“`C
int OCILogon2(OCISvcCtx **svchp, OCIEnv* envhp, OCIError* errhp, CONST OraText *usern, ub4 usernl, CONST OraText *passw, ub4 passwl, CONST OraText *dbname, ub4 dbnamel, CONST OraText *tag, ub4 tagl, CONST OraText *attribs, ub4 num_attribs, OCIAuthInfo *authhp, ub4 mode);
其中svchp表示服务句柄,envhp表示环境句柄,errhp表示错误句柄,usern表示连接用户名,usernl表示连接用户名长度,passw表示连接密码,passwl表示连接密码长度,dbname表示数据库名称,dbnamel表示数据库名称长度,tag表示标签,tagl表示标签长度,attribs表示属性,num_attribs表示属性数量,authhp表示认证信息,mode表示模式。成功创建连接后,将返回0。
下列代码以UTF-8编码演示了如何创建Oracle数据库连接:
```C#include
#include
#include
#include
#include
int mn(){
setlocale(LC_CTYPE, ""); OCIEnv *envhp;
OCIInitialize((ub4) OCI_DEFAULT,(dvoid *)0, (dvoid * (*)(dvoid *, size_t)) 0,(dvoid * (*)(dvoid *, dvoid *, size_t))0,(void (*)(dvoid *, dvoid *)) 0 ); OCIEnvInit(&envhp, OCI_DEFAULT, (size_t) 0, (dvoid **) 0);
OCISvcCtx *svchp; OCIError *errhp;
OCIHandleAlloc((dvoid *)envhp, (dvoid **)&svchp, OCI_HTYPE_SVCCTX, 0, (dvoid **)0); OCIHandleAlloc((dvoid *)envhp, (dvoid **)&errhp, OCI_HTYPE_ERROR, 0, (dvoid **)0);
OCILogon2(envhp, errhp, &svchp, (OraText *)"username",(ub4)strlen("username"), (OraText *)"password",(ub4)strlen("password"), (OraText *)"hostname",(ub4)strlen("hostname"), (OraText *)0,0, OCI_DEFAULT); printf("Connected!\n");
OCILogoff(svchp, errhp); OCIHandleFree((dvoid *)svchp, OCI_HTYPE_SVCCTX);
OCIHandleFree((dvoid *)errhp, OCI_HTYPE_ERROR); OCITerminate((ub4) OCI_DEFAULT, (dvoid *)0,
(dvoid * (*)(dvoid *, size_t)) 0, (dvoid * (*)(dvoid *, dvoid *, size_t)) 0,
(void (*)(dvoid *, dvoid *)) 0 ); return 0;
}
三、执行SQL查询
连接到Oracle数据库后,可以执行SQL查询,以检索数据或修改数据。要在C程序中执行一个SQL查询语句,可以使用以下函数:
“`C
int OCIStmtPrepare2(OCISvcCtx *svchp, OCIStmt **stmtp, OCIError *errhp, CONST OraText *stmttext, ub4 stmtt, CONST OraText *key, ub4 keyt, ub4 language, ub4 mode);
其中svchp代表服务句柄,stmtp表示语句句柄,errhp代表错误句柄,stmttext表示语句文本,stmtt表示语句长度,key表示关键词,keyt表示关键词长度,language表示语言类型,mode表示执行模式。执行成功后,将返回0。
下例演示如何执行SQL查询:
```C#include
#include
#include
#include
#include
int mn(){
setlocale(LC_CTYPE, ""); OCIEnv *envhp;
OCIInitialize((ub4) OCI_DEFAULT,(dvoid *)0, (dvoid * (*)(dvoid *, size_t)) 0,(dvoid * (*)(dvoid *, dvoid *, size_t))0,(void (*)(dvoid *, dvoid *)) 0 ); OCIEnvInit(&envhp, OCI_DEFAULT, (size_t) 0, (dvoid **) 0);
OCISvcCtx *svchp; OCIError *errhp;
OCIHandleAlloc((dvoid *)envhp, (dvoid **)&svchp, OCI_HTYPE_SVCCTX, 0, (dvoid **)0); OCIHandleAlloc((dvoid *)envhp, (dvoid **)&errhp, OCI_HTYPE_ERROR, 0, (dvoid **)0);
OCILogon2(envhp, errhp, &svchp, (OraText *)"username",(ub4)strlen("username"), (OraText *)"password",(ub4)strlen("password"), (OraText *)"hostname",(ub4)strlen("hostname"), (OraText *)0,0, OCI_DEFAULT);
OCIStmt *stmtp; char buffer[1024] = "SELECT * FROM CUSTOMERS";
OCIStmtPrepare2(svchp, &stmtp, errhp, (OraText*)buffer, (ub4)strlen(buffer), (OraText*)NULL, 0, OCI_NTV_SYNTAX, OCI_DEFAULT); OCIStmtExecute(svchp, stmtp, errhp, 0, 0, NULL, NULL, OCI_DEFAULT);
OCIRowid *row_id; char name[50];
OCIDefine *defnp1 = NULL, *defnp2 = NULL; OCIDefine *defnp3 = NULL, *defnp4 = NULL, *defnp5 = NULL, *defnp6 = NULL;
int id, age; double salary;
int intemp, ret; for (int i = 1; ; i++) {
ret = OCIStmtFetch2(stmtp, errhp, 1, OCI_DEFAULT, 0, OCI_DEFAULT); if (ret == OCI_NO_DATA) break;
OCIAttrGet(stmtp, OCI_HTYPE_STMT, (void *)&row_id, 0, OCI_ATTR_ROWID, errhp); OCIAttrGet(stmtp, OCI_HTYPE_STMT, (void *)&defnp1, i, OCI_ATTR_DESCRIBE_POSITION, errhp);
OCIAttrGet(defnp1, OCI_DTYPE_PARAM, (void *)&name, 0, OCI_ATTR_NAME, errhp); OCIAttrGet(stmtp, OCI_HTYPE_STMT, (void *)&defnp2, i, OCI_ATTR_DESCRIBE_POSITION, errhp);
OCIAttrGet(defnp2, OCI_DTYPE_PARAM, (void *)&id, 0, OCI_ATTR_DATA_TYPE, errhp); OCIAttrGet(stmtp, OCI_HTYPE_STMT, (void *)&defnp3, i, OCI_ATTR_DESCRIBE_POSITION, errhp);
OCIAttrGet(defnp3, OCI_DTYPE_PARAM, (void *)&age, 0, OCI_ATTR_DATA_TYPE, errhp); OCIAttrGet(stmtp, OCI_HTYPE_STMT, (void *)&defnp4, i, OCI_ATTR_DESCRIBE_POSITION, errhp);
OCIAttrGet(defnp4, OCI_DTYPE_PARAM, (void *)&salary, 0, OCI_ATTR_DATA_TYPE, errhp); printf("%s %d %d %f\n", name, id, age, salary);
} OCIStmtRelease(stmtp, errhp, NULL, 0, OCI_DEFAULT);
OCILogoff(svchp, errhp);
OCIHandleFree((dvoid *)svchp, OCI_HTYPE_SVCCTX); OCIHandleFree((dvoid *)errhp, OCI_HTYPE_ERROR);
OCITerminate((ub