掌握C语言,Oracle数据录入技巧
C语言是一种高效且功能强大的编程语言,广泛用于操作系统、嵌入式系统和游戏开发等领域。而Oracle则是世界著名的关系型数据库,被广泛应用于企业级的数据管理与分析。
在许多实际应用中,我们需要将C语言程序中的数据录入到Oracle数据库中,以进行后续的数据分析和处理。本文将介绍一些掌握C语言和Oracle数据录入的技巧,帮助开发者更加高效地完成这项任务。
一、连接Oracle数据库
在C语言中,连接Oracle数据库需要用到OCI(Oracle Call Interface)库,该库提供了一些基本的函数和操作来实现与Oracle数据库的交互。以下是一个简单的连接Oracle数据库的示例代码:
#include
#include
#include
int mn()
{
OCIEnv* envhp; // 环境句柄
OCIServer* srvhp; // 服务器句柄
OCIError* errhp; // 错误句柄
sword status; // 状态变量
// 初始化OCI环境
status = OCIEnvCreate(&envhp, OCI_THREADED, NULL, NULL, NULL, NULL, 0, NULL);
if (status != OCI_SUCCESS)
{
printf("Fled to initialize OCI environment.\n");
exit(EXIT_FLURE);
}
// 分配错误句柄
status = OCIHandleAlloc(envhp, (void**)&errhp, OCI_HTYPE_ERROR, 0, NULL);
if (status != OCI_SUCCESS)
{
printf("Fled to allocate error handle.\n");
exit(EXIT_FLURE);
}
// 分配服务器句柄
status = OCIHandleAlloc(envhp, (void**)&srvhp, OCI_HTYPE_SERVER, 0, NULL);
if (status != OCI_SUCCESS)
{
printf("Fled to allocate server handle.\n");
exit(EXIT_FLURE);
}
// 连接Oracle数据库
status = OCIServerAttach(srvhp, errhp, (OraText*)"ORCL", 4, OCI_DEFAULT);
if (status != OCI_SUCCESS)
{
printf("Fled to connect to Oracle database.\n");
exit(EXIT_FLURE);
}
printf("Connected to Oracle database successfully.\n");
// 释放资源
OCIHandleFree(srvhp, OCI_HTYPE_SERVER);
OCIHandleFree(errhp, OCI_HTYPE_ERROR);
OCIHandleFree(envhp, OCI_HTYPE_ENV);
return 0;
}
上述代码中,我们通过OCIEnvCreate函数初始化了OCI环境,通过OCIHandleAlloc函数分别分配了错误句柄和服务器句柄,然后通过OCIServerAttach函数连接了名为“ORCL”的Oracle数据库。我们通过OCIHandleFree函数释放了所有的资源。
二、录入数据到Oracle数据库
连接Oracle数据库后,我们可以使用OCI库提供的函数来将数据录入到数据库中。以下是一个简单的示例代码,演示了如何录入一个包含名字和年龄的人物信息到Oracle数据库中:
#include
#include
#include
int mn()
{
OCIEnv* envhp;
OCIServer* srvhp;
OCIError* errhp;
OCISession* usrhp;
OCISvcCtx* svchp;
OCIStmt* stmthp;
sword status;
char* name = "John";
int age = 30;
// 初始化OCI环境
status = OCIEnvCreate(&envhp, OCI_THREADED, NULL, NULL, NULL, NULL, 0, NULL);
if (status != OCI_SUCCESS)
{
printf("Fled to initialize OCI environment.\n");
exit(EXIT_FLURE);
}
// 分配错误句柄
status = OCIHandleAlloc(envhp, (void**)&errhp, OCI_HTYPE_ERROR, 0, NULL);
if (status != OCI_SUCCESS)
{
printf("Fled to allocate error handle.\n");
exit(EXIT_FLURE);
}
// 分配服务器句柄
status = OCIHandleAlloc(envhp, (void**)&srvhp, OCI_HTYPE_SERVER, 0, NULL);
if (status != OCI_SUCCESS)
{
printf("Fled to allocate server handle.\n");
exit(EXIT_FLURE);
}
// 连接Oracle数据库
status = OCIServerAttach(srvhp, errhp, (OraText*)"ORCL", 4, OCI_DEFAULT);
if (status != OCI_SUCCESS)
{
printf("Fled to connect to Oracle database.\n");
exit(EXIT_FLURE);
}
// 按照用户名和密码进行登录
status = OCIHandleAlloc(envhp, (void**)&usrhp, OCI_HTYPE_SESSION, 0, NULL);
if (status != OCI_SUCCESS)
{
printf("Fled to allocate user authentication handle.\n");
exit(EXIT_FLURE);
}
// 设置用户名和密码
status = OCIAttrSet(usrhp, OCI_HTYPE_SESSION, (void*)"scott", strlen("scott"),
OCI_ATTR_USERNAME, errhp);
if (status != OCI_SUCCESS)
{
printf("Fled to set username attribute.\n");
exit(EXIT_FLURE);
}
status = OCIAttrSet(usrhp, OCI_HTYPE_SESSION, (void*)"tiger", strlen("tiger"),
OCI_ATTR_PASSWORD, errhp);
if (status != OCI_SUCCESS)
{
printf("Fled to set password attribute.\n");
exit(EXIT_FLURE);
}
// 建立会话
status = OCISessionBegin(svc, errhp, usrhp, OCI_CRED_RDBMS, OCI_DEFAULT);
if (status != OCI_SUCCESS)
{
printf("Fled to start session.\n");
exit(EXIT_FLURE);
}
// 建立服务上下文
status = OCIHandleAlloc(envhp, (void**)&svchp, OCI_HTYPE_SVCCTX, 0, NULL);
if (status != OCI_SUCCESS)
{
printf("Fled to allocate service context handle.\n");
exit(EXIT_FLURE);
}
status = OCIAttrSet(svchp, OCI_HTYPE_SVCCTX, (void*)srvhp, 0,
OCI_ATTR_SERVER, errhp);
if (status != OCI_SUCCESS)
{
printf("Fled to set server attribute.\n");
exit(EXIT_FLURE);
}
status = OCIAttrSet(svchp, OCI_HTYPE_SVCCTX, (void*)usrhp, 0,
OCI_ATTR_SESSION, errhp);
if (status != OCI_SUCCESS)
{
printf("Fled to set session attribute.\n");
exit(EXIT_FLURE);
}
// 准备SQL语句
char* sql = "INSERT INTO person (name, age) VALUES (:name, :age)";
status = OCIHandleAlloc(envhp, (void**)&stmthp, OCI_HTYPE_STMT, 0, NULL);
if (status != OCI_SUCCESS)
{
printf("Fled to prepare SQL statement handle.\n");
exit(EXIT_FLURE);
}
status = OCIStmtPrepare(stmthp, errhp, (text*)sql, strlen(sql), OCI_NTV_SYNTAX, OCI_DEFAULT);
if (status != OCI_SUCCESS)
{
printf("Fled to prepare SQL statement.\n");
exit(EXIT_FLURE);
}
// 绑定参数
OCIBind* bindhp_name;
OCIBind* bindhp_age;
ub4 name_len = strlen(name);
ub4 age_len = sizeof(int);
status = OCIBindByName(stmthp, &bindhp_name, errhp, (text*)":name",
strlen(":name"), (dvoid*)name, name_len + 1, SQLT_STR,
NULL, NULL, NULL, 0, NULL, OCI_DEFAULT);
if (status != OCI_SUCCESS)
{
printf("Fled to bind name parameter.\n");
exit(EXIT_FLURE);
}
status = OCIBindByName(stmthp, &bindhp_age, errhp, (text*)":age",
strlen(":age"), (dvoid*)&age, age_len, SQLT_INT,
NULL, NULL, NULL, 0, NULL, OCI_DEFAULT);
if (status != OCI_SUCCESS)
{
printf("Fled to bind age parameter.\n");
exit(EXIT_FLURE);
}
// 执行SQL语句
status = OCIStmtExecute(svchp, stmthp, errhp, 1, 0, NULL, NULL,
OCI_DEFAULT);
if (status != OCI_SUCCESS)
{
printf("Fled to execute SQL statement.\n");
exit(EXIT_FLURE);
}
printf("Inserted data into Oracle database successfully.\n");
// 释放资源
OCIHandleFree(stmthp, OCI_H