C语言与Oracle数据库的结合(c 加oracle数据库)
C语言与Oracle数据库的结合
C语言是一种高效的编程语言,而Oracle数据库是一种广泛应用于企业级应用和大型网站的关系型数据库管理系统。将这两者结合,可以实现更加灵活和高效的数据处理和存储。下面我将介绍C语言与Oracle数据库的结合实例,并附上相关代码。
1. 数据库连接设置
在C语言中,我们需要使用Oracle提供的OCI接口连接到Oracle数据库。我们可以使用以下代码片段连接Oracle数据库:
“`c
OCIEnv *envhp;
OCIError *errhp;
OCISession *authp;
OCIServer *srvhp;
OCISvcCtx *svchp;
OCIStmt *stmthp;
char username[30] = “your_username”;
char password[30] = “your_password”;
char server_name[30] = “server_name”;
int server_port = 1521;
char service_name[30] = “service_name”;
OCIInitialize((ub4)OCI_DEFAULT, (dvoid *)0, (dvoid * (*)(dvoid *, size_t)) 0, (dvoid * (*)(dvoid *, dvoid *, size_t)) 0, (void (*)(dvoid *, dvoid *)) 0);
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);
OCIHandleAlloc((dvoid *) envhp, (dvoid **) &errhp, OCI_HTYPE_ERROR, (size_t) 0, (void **) 0);
OCIHandleAlloc((dvoid *) envhp, (dvoid **) &srvhp, OCI_HTYPE_SERVER, (size_t) 0, (void **) 0);
OCIServerAttach(srvhp, errhp, (text *)server_name, (sb4)strlen(server_name), (ub4)OCI_DEFAULT);
OCIHandleAlloc((dvoid *) envhp, (dvoid **) &svchp, OCI_HTYPE_SVCCTX, (size_t) 0, (void **) 0);
OCIAttrSet((dvoid *) svchp, OCI_HTYPE_SVCCTX, (dvoid *) srvhp, (ub4) 0, OCI_ATTR_SERVER, errhp);
OCIHandleAlloc((dvoid *) envhp, (dvoid **) &authp, OCI_HTYPE_SESSION, (size_t) 0, (void **) 0);
OCIAttrSet((dvoid *) authp, OCI_HTYPE_SESSION, (dvoid *) username, (ub4) strlen(username), OCI_ATTR_USERNAME, errhp);
OCIAttrSet((dvoid *) authp, OCI_HTYPE_SESSION, (dvoid *) password, (ub4) strlen(password), OCI_ATTR_PASSWORD, errhp);
OCIAttrSet((dvoid *) svchp, OCI_HTYPE_SVCCTX, (dvoid *) authp, (ub4) 0, OCI_ATTR_SESSION, errhp);
OCIHandleAlloc((dvoid *) envhp, (dvoid **) &stmthp, OCI_HTYPE_STMT, (size_t) 0, (void **) 0);
OCIStmtPrepare(stmthp, errhp, (text *) “select * from your_table_name”, (ub4) strlen(“select * from your_table_name”), (ub4) OCI_NTV_SYNTAX, (ub4) OCI_DEFAULT);
在以上代码片段中,我们首先使用OCIInitialize函数初始化OCI环境,然后使用OCIEnvCreate函数创建OCI环境句柄。接着,我们使用OCIHandleAlloc函数分配错误句柄errhp、服务器句柄srvhp、服务上下文句柄svchp、认证会话句柄authp和语句句柄stmthp。随后,我们使用OCIServerAttach函数连接到Oracle服务器,OCIAttrSet函数将服务器句柄和认证会话句柄设置到服务上下文句柄中,从而建立了与Oracle服务器的连接。我们使用OCIStmtPrepare函数准备了一条SQL查询语句。
2. 数据库查询操作
在C语言中,我们可以使用OCIStmtExecute函数执行SQL查询语句,如下所示:
```csword status;
ub4 nrows = 0;
status = OCIStmtExecute(svchp, stmthp, errhp, (ub4) 0, (ub4) 0, (CONST OCISnapshot *) NULL, (OCISnapshot *) NULL, OCI_DEFAULT);if (status != OCI_SUCCESS && status != OCI_SUCCESS_WITH_INFO) {
OCIErrorGet((dvoid *) errhp, (ub4) 1, (text *) NULL, &errcode, (text *) msgbuf, (ub4) sizeof(msgbuf), OCI_HTYPE_ERROR); printf("Error: %.*s", errcode, msgbuf);
return 1;}
OCIAttrGet((CONST dvoid *) stmthp, (ub4) OCI_HTYPE_STMT, (dvoid *) &nrows, (ub4 *) 0, (ub4) OCI_ATTR_ROW_COUNT, errhp);
if (nrows > 0) { printf("Result:\n");
OCIParam *colDesc; ub4 colCount;
ub4 i;
OCIAttrGet((CONST dvoid *) stmthp, (ub4) OCI_HTYPE_STMT, (dvoid *) &colCount, (ub4 *) 0, (ub4) OCI_ATTR_PARAM_COUNT, errhp);
for (i = 1; i OCIParamGet((dvoid *) stmthp, (ub4) OCI_HTYPE_STMT, errhp, (dvoid **) &colDesc, i);
printf("%.*s\t", 30, getFieldName(colDesc)); OCIHandleFree((dvoid *) colDesc, (ub4) OCI_HTYPE_DESC);
}
printf("\n---------------------------------------------------------\n");
for (i = 1; i OCIStmtFetch2(stmthp, errhp, (ub4) 1, OCI_FETCH_NEXT, (sb4) OCI_DEFAULT);
for (j = 1; j printf("%.*s\t", 30, getValue(stmthp, j));
} printf("\n");
}}
OCIHandleFree((dvoid *) stmthp, (ub4) OCI_HTYPE_STMT);OCIHandleFree((dvoid *) authp, (ub4) OCI_HTYPE_SESSION);
OCIHandleFree((dvoid *) svchp, (ub4) OCI_HTYPE_SVCCTX);OCIServerDetach(srvhp, errhp, OCI_DEFAULT);
OCIHandleFree((dvoid *) srvhp, (ub4) OCI_HTYPE_SERVER);OCIHandleFree((dvoid *) errhp, (ub4) OCI_HTYPE_ERROR);
OCIHandleFree((dvoid *) envhp, (ub4) OCI_HTYPE_ENV);
在以上代码片段中,我们首先使用OCIStmtExecute函数执行SQL查询语句,并将查询结果存储在OCIDefine句柄中。接着,我们使用OCIAttrGet函数获取查询结果中的行数和列数,然后遍历每一行和每一列,使用OCIStmtFetch2函数获取每一行数据,使用getValue函数和getFieldName函数获取每一列的数据和名称,并打印输出查询结果。我们通过OCIHandleFree函数释放所有的OCI句柄。
3. 数据库插入操作
我们可以使用以下代码从C语言中向Oracle数据库中插入数据:
“`c
ub4 row_count = 0;
char *insert_sql = “INSERT INTO your_table (col1, col2, col3) VALUES (:1, :2, :3)”;
OCIStmt *stmthp;
OCIParam *bindhp[3];
OCIHandleAlloc(envhp, (dvoid **) &stmthp, OCI_HTYPE_STMT, 0, NULL);
OCIStmtPrepare(stmthp, errhp, (text *) insert_sql, strlen(insert_sql), OCI_NTV_SYNTAX, OCI_DEFAULT);
for (int i = 0; i
OCIParamGet((dvoid *) stmthp,
(ub4) OCI_HTYPE_STMT,
errhp,
(dvoid **) &bindhp[i],
(ub4) (i + 1));
OCIBindByPos(stmthp,
&bindhp[i],
errhp,
(ub4) (i + 1),
NULL,
(sb4) 0,
SQLT_STR,
NULL,
NULL,
NULL,
0,
NULL,
OCI_DEFAULT);
char *value = i == 0 ? “value1” : i == 1 ? “value2” : “value3”;
OCIBindArrayOfStruct(bindhp[i],
errhp,
MAX_ARR_SIZE,
0,
sizeof(char *),