c语言实现Oracle数据入库(c oracle数据入库)

C语言实现Oracle数据入库

Oracle数据库是一款非常流行的企业级数据库管理系统之一,它提供了强大的数据库管理功能和高效的数据处理能力。如果我们希望使用C语言对Oracle数据库进行操作,例如向数据库中插入数据,我们可以使用OCI(Oracle Call Interface)接口实现。下面就是一个实例代码,可以帮助你使用C语言实现Oracle数据入库。

实现步骤:

1.在Oracle数据库中创建一个表

首先我们需要在Oracle数据库中创建一个表,用于存储我们将要插入的数据。下面是一个简单的SQL语句,用于创建一个名为“student”的表,这个表包含两个字段:id和name。

CREATE TABLE student (

id NUMBER(10),

name VARCHAR2(50)

);

2.编写C语言代码

我们可以使用C语言编写一个函数,用于将数据插入到表中。下面是一个示例代码:

#include

#include

#include //OCI调用接口

#define DB “test” //数据库名称

#define USER “user” //用户名

#define PASSWORD “password” //密码

void insert_student(int id, char* name) {

OCIEnv *envhp;

OCIServer *srvhp;

OCIError *errhp;

OCISession *authp;

OCISvcCtx *svchp;

OCIStmt *stmtp;

OCIBind *bndhp1, *bndhp2;

text *insert = (text *)”INSERT INTO student VALUES (:id, :name)”;

ub2 id_len, name_len;

sword status;

sb2 indp;

// 初始化OCI环境

OCIEnvCreate(&envhp, OCI_DEFAULT, (dvoid *)0, (dvoid * (*)(dvoid *, size_t)) 0,

(dvoid * (*)(dvoid *, dvoid *, size_t))0, (void (*)(dvoid *, dvoid *)) 0,

(size_t) 0, (dvoid **)0);

// 创建OCI错误处理器

OCIHandleAlloc((dvoid *)envhp, (dvoid **)&errhp, OCI_HTYPE_ERROR,

(size_t)0, (dvoid **)0);

// 创建OCI服务器句柄

OCIHandleAlloc((dvoid *)envhp, (dvoid **)&srvhp, OCI_HTYPE_SERVER,

(size_t)0, (dvoid **)0);

// 创建OCI服务句柄

OCIHandleAlloc((dvoid *)envhp, (dvoid **)&svchp, OCI_HTYPE_SVCCTX,

(size_t)0, (dvoid **)0);

// 启动OCI服务器

OCIServerAttach(srvhp, errhp, (text *)DB, strlen(DB), OCI_DEFAULT);

// 初始化OCI服务上下文

OCIAttrSet((dvoid *)svchp, OCI_HTYPE_SVCCTX, (dvoid *)srvhp, (ub4)0,

OCI_ATTR_SERVER, errhp);

// 创建OCI会话

OCIHandleAlloc((dvoid *)envhp, (dvoid **)&authp, OCI_HTYPE_SESSION,

(size_t)0, (dvoid **)0);

// 设置会话用户名和密码

OCIAttrSet((dvoid *)authp, OCI_HTYPE_SESSION, (dvoid *)USER, (ub4)strlen(USER),

OCI_ATTR_USERNAME, errhp);

OCIAttrSet((dvoid *)authp, OCI_HTYPE_SESSION, (dvoid *)PASSWORD, (ub4)strlen(PASSWORD),

OCI_ATTR_PASSWORD, errhp);

// 开启OCI会话

OCISessionBegin(svchp, errhp, authp, OCI_CRED_RDBMS, OCI_DEFAULT);

//Asm OCI_STMT_SCROLLABLE_READONLY for read only cursor.

OCIHandleAlloc(envhp, (dvoid **)&stmtp, OCI_HTYPE_STMT, (size_t)0, (dvoid **)0);

// 绑定参数

OCIHandleAlloc((dvoid *)envhp, (dvoid **)&bndhp1, OCI_HTYPE_BIND,

(size_t)0, (dvoid **)0);

id_len = (ub2)strlen(name);

OCIAttrSet((dvoid *)bndhp1, OCI_HTYPE_BIND, (dvoid *)&id, (ub4)sizeof(int),

OCI_ATTR_DATA_TYPE, errhp);

OCIAttrSet((dvoid *)bndhp1, OCI_HTYPE_BIND, (dvoid *)&indp, (ub4)sizeof(indp),

OCI_ATTR_INDICATOR, errhp);

OCIAttrSet((dvoid *)bndhp1, OCI_HTYPE_BIND, (dvoid *)&id_len, (ub4)sizeof(id_len),

OCI_ATTR_MAXDATA_SIZE, errhp);

OCIAttrSet((dvoid *)bndhp1, OCI_HTYPE_BIND, (dvoid *)&id_len, (ub4)sizeof(id_len),

OCI_ATTR_DATA_SIZE, errhp);

OCIHandleAlloc((dvoid *)envhp, (dvoid **)&bndhp2, OCI_HTYPE_BIND,

(size_t)0, (dvoid **)0);

name_len = (ub2)strlen(name);

OCIAttrSet((dvoid *)bndhp2, OCI_HTYPE_BIND, (dvoid *)name, (ub4)strlen(name),

OCI_ATTR_DATA_TYPE, errhp);

OCIAttrSet((dvoid *)bndhp2, OCI_HTYPE_BIND, (dvoid *)&indp, (ub4)sizeof(indp),

OCI_ATTR_INDICATOR, errhp);

OCIAttrSet((dvoid *)bndhp2, OCI_HTYPE_BIND, (dvoid *)&name_len, (ub4)sizeof(name_len),

OCI_ATTR_MAXDATA_SIZE, errhp);

OCIAttrSet((dvoid *)bndhp2, OCI_HTYPE_BIND, (dvoid *)&name_len, (ub4)sizeof(name_len),

OCI_ATTR_DATA_SIZE, errhp);

// 执行插入操作

OCIStmtPrepare(stmtp, errhp, insert, strlen((char *)insert), OCI_NTV_SYNTAX,

OCI_DEFAULT);

OCIStmtBindByPos(stmtp, &bndhp1, errhp, 1, (dvoid *)&id, (sb4)sizeof(int),

SQLT_INT, (dvoid *)&indp, (ub2 *)0, (ub2 *)0, (ub4)0,

(ub4 *)0, OCI_DEFAULT);

OCIStmtBindByPos(stmtp, &bndhp2, errhp, 2, (dvoid *)name, (sb4)strlen(name),

SQLT_STR, (dvoid *)&indp, (ub2 *)&name_len, (ub2 *)0, (ub4)0,

(ub4 *)0, OCI_DEFAULT);

OCIStmtExecute(svchp, stmtp, errhp, (ub4)1, (ub4)0, (CONST OCISnapshot *)NULL,

(OCISnapshot *)NULL, OCI_COMMIT_ON_SUCCESS);

// 释放OCI资源

OCIHandleFree((dvoid *)stmtp, OCI_HTYPE_STMT);

OCIHandleFree((dvoid *)bndhp1, OCI_HTYPE_BIND);

OCIHandleFree((dvoid *)bndhp2, OCI_HTYPE_BIND);

OCISessionEnd(svchp, errhp, authp, OCI_DEFAULT);

OCIHandleFree((dvoid *)authp, OCI_HTYPE_SESSION);

OCIServerDetach(srvhp, errhp, OCI_DEFAULT);

OCIHandleFree((dvoid *)srvhp, OCI_HTYPE_SERVER);

OCIHandleFree((dvoid *)svchp, OCI_HTYPE_SVCCTX);

OCIHandleFree((dvoid *)errhp, OCI_HTYPE_ERROR);

OCIHandleFree((dvoid *)envhp, OCI_HTYPE_ENV);

}

在这个示例代码中,我们使用OCI接口来实现向Oracle数据库的student表中插入数据的功能。这段代码将会连接到指定的数据库,创建一个会话来执行SQL查询,然后使用OCIStmtBindByPos函数将参数绑定到SQL语句中的占位符。我们调用OCIStmtExecute函数来执行INSERT语句,并提交事务以确保数据被成功插入。

3.编译并执行代码

使用gcc编译这段代码:

gcc -o insert_student insert_student.c -lclntsh

执行编译后的程序:

./insert_student

在控制台中输入要插入到student表中的数据,例如:

Please enter student id: 1

Please enter student name: John

如果上述操作顺利完成,则数据已经成功插入到Oracle数据库中的student表中。

总结

本文介绍了如何使用C语言实现Oracle数据入库。我们首先需要在Oracle数据库中创建一个表,然后使用OCI接口编写C程序,通过这个程序向数据库


数据运维技术 » c语言实现Oracle数据入库(c oracle数据入库)