在这个例子中,我们将使用C Orion API(Oracle内部API)来连接到Oracle数据库。首先需要确保安装了Oracle数据库和C开发环境。其中OCI(Oracle Call Interface)是用于在C语言中操作Oracle的最基本的API之一,其为C语言提供了访问Oracle数据库的接口。在接下来的例子中,我们将使用OCI的一些基本功能:
#include#include #include #include int main() { OCIEnv *envhp; OCIError *errhp; OCIServer *srvhp; OCISvcCtx *svchp; OCISession *sesshp; text *username = (text *)"username"; text *password = (text *)"password"; text *dbname = (text *)"dbname"; sword retval; char errbuf[512]; retval = OCIEnvCreate(&envhp, OCI_DEFAULT, NULL, NULL, NULL, NULL, 0, NULL); if (retval != OCI_SUCCESS) { printf("OCIEnvCreate failed with error %d", retval); exit(1); } retval = OCIHandleAlloc((dvoid *)envhp, (dvoid **)&errhp, OCI_HTYPE_ERROR, 0, NULL); if (retval != OCI_SUCCESS) { printf("OCIHandleAlloc failed with error %d", retval); exit(1); } retval = OCIHandleAlloc((dvoid *)envhp, (dvoid **)&srvhp, OCI_HTYPE_SERVER, 0, NULL); if (retval != OCI_SUCCESS) { printf("OCIHandleAlloc failed with error %d", retval); exit(1); } retval = OCIHandleAlloc((dvoid *)envhp, (dvoid **)&svchp, OCI_HTYPE_SVCCTX, 0, NULL); if (retval != OCI_SUCCESS) { printf("OCIHandleAlloc failed with error %d", retval); exit(1); } retval = OCIServerAttach(srvhp, errhp, (text *)dbname, strlen((char *)dbname), OCI_DEFAULT); if (retval != OCI_SUCCESS) { OCIErrorGet((dvoid *)errhp, (ub4)1, (text *)NULL, &retval, (text *)errbuf, (ub4)sizeof(errbuf), OCI_HTYPE_ERROR); printf("Failed to attach to server with error %s", errbuf); exit(1); } retval = OCIAttrSet((dvoid *)svchp, OCI_HTYPE_SVCCTX, (dvoid *)srvhp, (ub4)0, OCI_ATTR_SERVER, errhp); if (retval != OCI_SUCCESS) { OCIErrorGet((dvoid *)errhp, (ub4)1, (text *)NULL, &retval, (text *)errbuf, (ub4)sizeof(errbuf), OCI_HTYPE_ERROR); printf("Failed to set server attribute with error %s", errbuf); exit(1); } retval = OCIHandleAlloc((dvoid *)envhp, (dvoid **)&sesshp, OCI_HTYPE_SESSION, 0, NULL); if (retval != OCI_SUCCESS) { printf("OCIHandleAlloc failed with error %d", retval); exit(1); } retval = OCIAttrSet((dvoid *)sesshp, OCI_HTYPE_SESSION, (dvoid *)username, (ub4)strlen((char *)username), OCI_ATTR_USERNAME, errhp); if (retval != OCI_SUCCESS) { OCIErrorGet((dvoid *)errhp, (ub4)1, (text *)NULL, &retval, (text *)errbuf, (ub4)sizeof(errbuf), OCI_HTYPE_ERROR); printf("Failed to set username attribute with error %s", errbuf); exit(1); } retval = OCIAttrSet((dvoid *)sesshp, OCI_HTYPE_SESSION, (dvoid *)password, (ub4)strlen((char *)password), OCI_ATTR_PASSWORD, errhp); if (retval != OCI_SUCCESS) { OCIErrorGet((dvoid *)errhp, (ub4)1, (text *)NULL, &retval, (text *)errbuf, (ub4)sizeof(errbuf), OCI_HTYPE_ERROR); printf("Failed to set password attribute with error %s", errbuf); exit(1); } retval = OCISessionBegin(svchp, errhp, sesshp, OCI_CRED_RDBMS, OCI_DEFAULT); if (retval != OCI_SUCCESS) { OCIErrorGet((dvoid *)errhp, (ub4)1, (text *)NULL, &retval, (text *)errbuf, (ub4)sizeof(errbuf), OCI_HTYPE_ERROR); printf("Failed to begin session with error %s", errbuf); exit(1); } retval = OCIAttrSet((dvoid *)svchp, OCI_HTYPE_SVCCTX, (dvoid *)sesshp, (ub4)0, OCI_ATTR_SESSION, errhp); if (retval != OCI_SUCCESS) { OCIErrorGet((dvoid *)errhp, (ub4)1, (text *)NULL, &retval, (text *)errbuf, (ub4)sizeof(errbuf), OCI_HTYPE_ERROR); printf("Failed to set session attribute with error %s", errbuf); exit(1); } printf("Oracle session established successfully\n"); }
在上述代码中,我们首先创建一个OCI环境对象并为其分配错误处理对象、服务对象、服务上下文对象和会话对象,然后使用OCI连接API连接到Oracle服务器,并使用OCI属性API设置会话的用户名和密码,最后启动会话并将其绑定到服务上下文对象。如果顺利的话,将会在控制台上输出 "Oracle session established successfully"。
可以在此基础上进一步添加OCI操作API,来对Oracle实例执行增删改查等操作。
在本文中,我们介绍了如何使用C语言来连接Oracle数据库。使用OCI提供了C语言程序员在Oracle中进行开发的便捷途径,它提供了一组完整的API,可以与Oracle进行交互,从而实现以下目标:
- 连接Oracle服务
- 管理会话对象
- 执行SQL语句
- 获取和更新数据
OCI提供了连接到Oracle实例、管理Oracle会话以及执行包括增删改查在内的操作的最基本工具。在这个基础上,我们可以进一步使用其他API和功能,来满足更为复杂的需求。