C程序实现Oracle操作(c 实现oracle)
C程序实现Oracle操作
在现代化的企业级应用开发中,使用Oracle数据库的公司并不鲜见。它的功能强大,可运行的应用程序数量很大,使用了许多开发语言。其中,C程序是一个广泛使用的语言,因为它快速、轻便且可以直接访问底层系统资源。然而,许多开发人员发现,在使用C程序时访问Oracle数据库是一件有些棘手的事情。今天,我们将介绍一些方法,帮助您轻松地在C程序中执行Oracle数据库操作。
前置条件
在访问Oracle数据库之前,我们需要下载和安装Oracle客户端。可以从Oracle官方网站下载适用于不同平台的适当版本。步骤如下:
1.访问https://www.oracle.com/technetwork/database/enterprise-edition/downloads/index.html。
2.选择正确的版本(Microsoft Windows系统或Linux等),然后单击下载。
3.在安装过程中,根据安装向导的提示进行操作。需要注意的是,请确保在安装过程中选择所有必要的选项,包括Net Service Name、Oracle Home和TNS Names等。
连接到Oracle数据库
在C程序中连接到Oracle数据库,需要使用Oracle提供的OCI库。OCI(Oracle Call Interface)是一个C语言编写的本机库,它允许开发人员编写C语言程序来访问Oracle数据库。
下面是一个连接Oracle数据库的简单C程序。它使用连接字符串、用户名和密码等参数连接到Oracle数据库,并检查连接是否成功:
#include
#include
#include
int mn(int argc, char *argv[]){
OCIEnv *envhp; OCIServer *srvhp;
OCIError *errhp; OCISession *usrhp;
OCISvcCtx *svchp;
sword status;
char *username = "your_username"; char *password = "your_password";
char *dbname = "//your_hostname:port/your_service_name"; //Connection string
status = OCIEnvCreate(&envhp, OCI_DEFAULT, (void *)0, (void *(*)(void *, size_t))0, (void *(*)(void *, void *, size_t))0, (void (*)(void *, void *))0, (size_t)0, (void **)0); if (status != OCI_SUCCESS)
{ printf("Error in OCIEnvCreate.");
exit(-1); }
status = OCIHandleAlloc((dvoid *)envhp, (dvoid **)&errhp, OCI_HTYPE_ERROR, (size_t)0, (dvoid **)0); if (status != OCI_SUCCESS)
{ printf("Error in OCIHandleAlloc.");
exit(-1); }
status = OCIHandleAlloc((dvoid *)envhp, (dvoid **)&srvhp, OCI_HTYPE_SERVER, (size_t)0, (dvoid **)0); if (status != OCI_SUCCESS)
{ printf("Error in OCIHandleAlloc.");
exit(-1); }
(void)OCIAttrSet((dvoid *)srvhp, OCI_HTYPE_SERVER, (dvoid *)dbname, (ub4)strlen((const char *)dbname), OCI_ATTR_SERVER_DNS, errhp);
status = OCIHandleAlloc((dvoid *)envhp, (dvoid **)&svchp, OCI_HTYPE_SVCCTX, (size_t)0, (dvoid **)0); if (status != OCI_SUCCESS)
{ printf("Error in OCIHandleAlloc.");
exit(-1); }
(void)OCIAttrSet((dvoid *)svchp, OCI_HTYPE_SVCCTX, (dvoid *)srvhp, (ub4)0, OCI_ATTR_SERVER, errhp);
status = OCIHandleAlloc((dvoid *)envhp, (dvoid **)&usrhp, OCI_HTYPE_SESSION, (size_t)0, (dvoid **)0); if (status != OCI_SUCCESS)
{ printf("Error in OCIHandleAlloc.");
exit(-1); }
(void)OCIAttrSet((dvoid *)usrhp, OCI_HTYPE_SESSION, (dvoid *)username, (ub4)strlen((const char *)username), OCI_ATTR_USERNAME, errhp); (void)OCIAttrSet((dvoid *)usrhp, OCI_HTYPE_SESSION, (dvoid *)password, (ub4)strlen((const char *)password), OCI_ATTR_PASSWORD, errhp);
status = OCISessionBegin(svchp, errhp, usrhp, OCI_CRED_RDBMS, OCI_DEFAULT); if (status != OCI_SUCCESS)
{ printf("Error in OCISessionBegin.");
exit(-1); }
status = OCIAttrSet((dvoid *)svchp, OCI_HTYPE_SVCCTX, (dvoid *)usrhp, (ub4)0, OCI_ATTR_SESSION, errhp); if (status != OCI_SUCCESS)
{ printf("Error in OCIAttrSet.");
exit(-1); }
printf("Connected to database successfully.");
return 0;}
执行SQL查询
一旦连接到Oracle数据库,我们可以执行SQL查询语句并检索数据。下面是一个简单的程序,从employees表中检索所有员工的姓名和工资:
#include
#include
#include
#include
int mn(int argc, char *argv[]){
OCIEnv *envhp; OCIServer *srvhp;
OCIError *errhp; OCISession *usrhp;
OCISvcCtx *svchp; OCIStmt *stmthp;
OCIDefine *defhp;
sword status;
char *username = "your_username"; char *password = "your_password";
char *dbname = "//your_hostname:port/your_service_name"; //Connection string
char *select_query = "SELECT first_name, last_name, salary FROM employees";
status = OCIEnvCreate(&envhp, OCI_DEFAULT, (void *)0, (void *(*)(void *, size_t))0, (void *(*)(void *, void *, size_t))0, (void (*)(void *, void *))0, (size_t)0, (void **)0); if (status != OCI_SUCCESS)
{ printf("Error in OCIEnvCreate.");
exit(-1); }
status = OCIHandleAlloc((dvoid *)envhp, (dvoid **)&errhp, OCI_HTYPE_ERROR, (size_t)0, (dvoid **)0); if (status != OCI_SUCCESS)
{ printf("Error in OCIHandleAlloc.");
exit(-1); }
status = OCIHandleAlloc((dvoid *)envhp, (dvoid **)&srvhp, OCI_HTYPE_SERVER, (size_t)0, (dvoid **)0); if (status != OCI_SUCCESS)
{ printf("Error in OCIHandleAlloc.");
exit(-1); }
(void)OCIAttrSet((dvoid *)srvhp, OCI_HTYPE_SERVER, (dvoid *)dbname, (ub4)strlen((const char *)dbname), OCI_ATTR_SERVER_DNS, errhp);
status = OCIHandleAlloc((dvoid *)envhp, (dvoid **)&svchp, OCI_HTYPE_SVCCTX, (size_t)0, (dvoid **)0); if (status != OCI_SUCCESS)
{ printf("Error in OCIHandleAlloc.");
exit(-1); }
(void)OCIAttrSet((dvoid *)svchp, OCI_HTYPE_SVCCTX, (dvoid *)srvhp, (ub4)0, OCI_ATTR_SERVER, errhp);
status = OCIHandleAlloc((dvoid *)envhp, (dvoid **)&usrhp, OCI_HTYPE_SESSION, (size_t)0, (dvoid **)0); if (status != OCI_SUCCESS)
{ printf("Error in OCIHandleAlloc.");
exit(-1); }
(void)OCIAttrSet((dvoid *)usrhp, OCI_HTYPE_SESSION, (dvoid *)username, (ub4)strlen((const char *)username), OCI_ATTR_USERNAME, errhp); (void)OCIAttrSet((dvoid *)usrhp, OCI_HTYPE_SESSION, (dvoid *)password, (ub4)strlen((const char *)password), OCI_ATTR_PASSWORD, errhp);
status = OCISessionBegin(svchp, errhp, usrhp, OCI_CRED_RDBMS, OCI_DEFAULT); if (status != OCI_SUCCESS)
{ printf("Error in OCISessionBegin.");
exit(-1); }
status = OCIAttrSet((dvoid *)svchp, OCI_HTYPE_SVCCTX, (dvoid *)usrhp, (ub4)0, OCI_ATTR_SESSION, errhp); if (status != OCI_SUCCESS)
{ printf("Error in OCIAttrSet.");
exit(-1); }
status = OCIHandleAlloc((dvoid *)envhp, (dvoid **)&stmthp, OCI_HTYPE_STMT, (size_t)0