C语言调用Oracle实现数据插入(c 调用oracle插入)
C语言调用Oracle实现数据插入
Oracle是一款商业关系型数据库管理系统,被广泛应用于企业级应用程序开发中。与此同时,C语言是一门受欢迎的高级编程语言,常常被用来开发应用程序、操作系统和嵌入式系统等。
在本文中,我们将探讨如何使用C语言调用Oracle数据库,实现数据插入操作。
1. 下载Oracle Instant Client
在开始编写代码之前,我们需要先下载Oracle Instant Client,在官方网站上下载适用于自己操作系统版本的客户端程序。安装完成后,使用如下代码将所需头文件引入:
#include
#include
#include
#include
2. 建立连接
在使用OCI(Database Call Interface)进行操作之前,先需要初始化Oracle环境,并建立到数据库的连接。下面是相关代码:
int mn()
{
OCIEnv *envhp; /* 程序环境句柄 */
OCIError *errhp; /* 错误句柄 */
OCISvcCtx *svchp; /* 服务上下文句柄 */
OCIStmt *stmthp; /* SQL语句句柄 */
OCIDefine *defhp; /* 取值句柄 */
OCIBind *bndhp; /* 绑定变量句柄 */
OCIServer *srvhp; /* 服务器句柄 */
OCISession *usrhp; /* 用户会话句柄 */
OCIResult *resulthp; /* OCI调用结果句柄 */
otext username[] = “username”; /* 用户名 */
otext password[] = “password”; /* 密码 */
otext dbname[] = “dbname”; /* 数据库名 */
oci8_handle_t service; /* 服务句柄 */
ub4 status; /* 状态代码 */
ub1 icharset_rep[40]; /* 字符集名称 */
ub4 i, count = 0;
OCIInitialize(OCI_DEFAULT, 0, 0, 0, 0);
OCIEnvInit(&envhp, OCI_DEFAULT, 0, 0);
OCIHandleAlloc((void *)envhp, (void **)&errhp, OCI_HTYPE_ERROR, 0, 0);
OCILogon2(
envhp,
errhp,
&svc,
(const OraText *)username, sizeof(username),
(const OraText *)password, sizeof(password),
(const OraText *)dbname, sizeof(dbname),
OCI_SESSION_DEFAULT
);
/* 创建会话 */
OCIHandleAlloc((void *)envhp, (void **)&usrhp, OCI_HTYPE_SESSION, 0, 0);
OCIAttrSet(
(void *)usrhp,
OCI_HTYPE_SESSION,
(void *)username, sizeof(username),
OCI_ATTR_USERNAME,
errhp
);
OCIAttrSet(
(void *)usrhp,
OCI_HTYPE_SESSION,
(void *)password, sizeof(password),
OCI_ATTR_PASSWORD,
errhp
);
checkerr(errhp, OCISessionBegin(svc, errhp, usrhp, OCI_CRED_RDBMS, OCI_DEFAULT));
OCIHandleFree((void *)usrhp, OCI_HTYPE_SESSION);
/* 创建服务器句柄 */
OCIHandleAlloc((void *)envhp, (void **)&srvhp, OCI_HTYPE_SERVER, 0, 0);
OCIAttrSet((void *)srvhp, OCI_HTYPE_SERVER, dbname, strlen(dbname), OCI_ATTR_SERVER_NAME, errhp);
checkerr(errhp, OCIServerAttach(srvhp, errhp, (OraText *)0, 0, (ub4)OCI_DEFAULT));
/* 创建服务上下文句柄 */
OCIHandleAlloc((void *)envhp, (void **)&svchp, OCI_HTYPE_SVCCTX, 0, 0);
OCIAttrSet((void *)svchp, OCI_HTYPE_SVCCTX, (void *)srvhp, 0, OCI_ATTR_SERVER, errhp);
/* 设置SESSION上下文 */
OCIAttrSet(
(void *)svchp,
OCI_HTYPE_SVCCTX,
(void *)usrhp,
0,
OCI_ATTR_SESSION,
errhp
);
OCIHandleAlloc((void *)envhp, (void **)&stmthp, OCI_HTYPE_STMT, 0, 0);
OCIStmtPrepare(stmthp, errhp, (const text *)”INSERT INTO T(ID, NAME) VALUES(:ID, :NAME)”, strlen(“INSERT INTO T(ID, NAME) VALUES(:ID, :NAME)”), OCI_NTV_SYNTAX, OCI_DEFAULT);
OCIHandleAlloc((void *)envhp, (void **)&bndhp, OCI_HTYPE_BIND, 0, 0);
OCIAttrSet((void *)bndhp, OCI_HTYPE_BIND, &bind_id, sizeof(int), OCI_ATTR_DATA_TYPE, errhp);
OCIAttrSet((void *)bndhp, OCI_HTYPE_BIND, &v_name, sizeof(v_name), OCI_ATTR_DATA_TYPE, errhp);
OCIAttrSet((void *)bndhp, OCI_HTYPE_BIND, &v_len, 2, OCI_ATTR_DATA_SIZE, errhp);
OCIAttrSet((void *)bndhp, OCI_HTYPE_BIND, &v_ind, 1, OCI_ATTR_INDICATOR, errhp);
OCIAttrSet((void *)bndhp, OCI_HTYPE_BIND, &v_len, 2, OCI_ATTR_MAXDATA_SIZE, errhp);
checkerr(
errhp,
OCIBindByName(
stmthp, &bndhp, errhp,
(const text *):”ID”, strlen(“ID”), NULL, 0,
SQLT_CHR, &bind_id, sizeof(int),
NULL, NULL, 0, NULL, OCI_DEFAULT
)
);
checkerr(
errhp,
OCIBindByName(
stmthp, &bndhp, errhp,
(const text *):”NAME”, strlen(“NAME”), NULL, 0,
SQLT_STR, &v_name, sizeof(v_name),
NULL, NULL, 0, NULL, OCI_DEFAULT)
);
/* 绑定变量 */
bind_id = 1;
strncpy(v_name, “name”, sizeof(v_name));
/* 执行语句 */
checkerr(errhp, OCIStmtExecute(svchp, stmthp, errhp, 1, 0, NULL, NULL, OCI_COMMIT_ON_SUCCESS));
/* 释放资源 */
OCIStmtRelease(stmthp, errhp, NULL, 0, OCI_DEFAULT);
OCILogoff(svc, errhp);
OCIHandleFree((void *)envhp, OCI_HTYPE_ENV);
}
3. 操作数据
在建立好连接之后,我们可以用OCI操作数据库。下面是一个简单的数据插入示例:
OCIStmtPrepare(
stmthp,
errhp,
(const text *)”INSERT INTO T(ID, NAME) VALUES(:ID, :NAME)”,
strlen(“INSERT INTO T(ID, NAME) VALUES(:ID, :NAME)”),
OCI_NTV_SYNTAX,
OCI_DEFAULT
);
OCIBindByName(
stmthp, &bndhp, errhp,
(const text *):”ID”, strlen(“ID”), NULL, 0,
SQLT_CHR, &bind_id, sizeof(int),
NULL, NULL, 0, NULL, OCI_DEFAULT
);
OCIBindByName(
stmthp, &bndhp, errhp,
(const text *):”NAME”, strlen(“NAME”), NULL, 0,
SQLT_STR, &v_name, sizeof(v_name),
NULL, NULL, 0, NULL, OCI_DEFAULT
);
/* 绑定变量 */
bind_id = 1;
strncpy(v_name, “name”, sizeof(v_name));
/* 执行语句 */
OCIStmtExecute(svchp, stmthp, errhp, 1, 0, NULL, NULL, OCI_COMMIT_ON_SUCCESS);
在这个例子中,我们使用OCIStmtPrepare函数预编译了一个SQL语句,然后使用OCIBindByName函数将变量绑定到明确的位置,并设置变量的值。最终,我们使用OCIStmtExecute函数执行语句。
4. 释放资源
在操作完成后,我们需要释放所有的资源,包括连接句柄、语句句柄和错误句柄等。相关代码如下所示:
/* 释放资源 */
OCIStmtRelease(stmthp, errhp, NULL, 0, OCI_DEFAULT);
OCILogoff(svc, err