生成c语言代码 一段oracel oci连接数据库的crud代码
时间: 2023-08-31 14:20:34 浏览: 246
### 回答1:
在连接 Oracle 数据库之前,首先需要在你的系统上安装 OCI 驱动。
下面是一个使用 OCI 连接到 Oracle 数据库并执行 CRUD 操作的 C 语言代码示例:
```
#include <stdio.h>
#include <oci.h>
int main()
{
/* 声明 OCI 句柄 */
OCIEnv *env;
OCIServer *server;
OCIError *err;
OCISession *session;
OCISvcCtx *svc;
OCIStmt *stmt;
/* 初始化 OCI 环境 */
OCIEnvCreate(&env, OCI_DEFAULT, (dvoid *)0, 0, 0, 0, 0, (dvoid **)0);
OCIHandleAlloc((dvoid *)env, (dvoid **)&err, OCI_HTYPE_ERROR, (size_t)0, (dvoid **)0);
/* 连接到 Oracle 数据库 */
OCIHandleAlloc((dvoid *)env, (dvoid **)&server, OCI_HTYPE_SERVER, (size_t)0, (dvoid **)0);
OCIServerAttach(server, err, (text *)"dbname", strlen("dbname"), OCI_DEFAULT);
OCIHandleAlloc((dvoid *)env, (dvoid **)&session, OCI_HTYPE_SESSION, (size_t)0, (dvoid **)0);
OCIAttrSet((dvoid *)session, OCI_HTYPE_SESSION, (dvoid *)&user, strlen(user), OCI_ATTR_USERNAME, err);
OCIAttrSet((dvoid *)session, OCI_HTYPE_SESSION, (dvoid *)&password, strlen(password), OCI_ATTR_PASSWORD, err);
OCISessionBegin(svc, err, session, OCI_CRED_RDBMS, OCI_DEFAULT);
OCIAttrSet((dvoid *)svc, OCI_HTYPE_SVCCTX, (dvoid *)server, (ub4)0, OCI_ATTR_SERVER, err);
OCIAttrSet((dvoid *)svc, OCI_HTYPE_SVCCTX, (dvoid *)session, (ub4)0, OCI_ATTR_SESSION, err);
/* 创建 SQL 语句 */
text *insert_stmt = "INSERT INTO mytable (id, name) VALUES (:1, :2)";
text *select_stmt = "SELECT name FROM mytable WHERE id = :1";
text *update_stmt = "UPDATE mytable SET name = :2 WHERE
### 回答2:
下面是一段基于Oracle OCI(Oracle Call Interface)的C语言代码,用于连接数据库和进行 CRUD(Create, Read, Update, Delete)操作:
```c
#include <stdio.h>
#include <stdlib.h>
#include <oci.h>
int main() {
// 声明OCI变量
OCIEnv *env;
OCIServer *server;
OCISession *session;
OCIError *err;
// 初始化OCI环境
OCIEnvCreate(&env, OCI_DEFAULT, NULL, NULL, NULL, NULL, 0, NULL);
// 创建错误处理句柄
OCIHandleAlloc(env, (void **)&err, OCI_HTYPE_ERROR, 0, NULL);
// 连接到数据库
OCIHandleAlloc(env, (void **)&server, OCI_HTYPE_SERVER, 0, NULL);
OCIServerAttach(server, err, (const OraText *)"<数据库地址>", strlen("<数据库地址>"), 0);
// 创建会话
OCIHandleAlloc(env, (void **)&session, OCI_HTYPE_SESSION, 0, NULL);
OCISessionBegin(env, err, session, OCI_CRED_RDBMS, OCI_DEFAULT, (const OraText *)"<用户名>", strlen("<用户名>"), (const OraText *)"<密码>", strlen("<密码>"));
// 设置会话
OCIAttrSet((void *)session, OCI_HTYPE_SESSION, server, 0, OCI_ATTR_SERVER, err);
// 准备语句
OCIStmt *stmt;
OCIHandleAlloc(env, (void **)&stmt, OCI_HTYPE_STMT, 0, NULL);
OCIStmtPrepare(stmt, err, (const OraText *)"SELECT * FROM <表名>", strlen("SELECT * FROM <表名>"), OCI_NTV_SYNTAX, OCI_DEFAULT);
// 执行查询
OCIStmtExecute(server, stmt, err, 1, 0, NULL, NULL, OCI_DEFAULT);
// 处理结果集
OCIParam *param = NULL;
OCIAttrGet(stmt, OCI_HTYPE_STMT, ¶m, 0, OCI_ATTR_PARAM_COUNT, err);
int numCols;
OCIAttrGet(param, OCI_DTYPE_PARAM, &numCols, 0, OCI_ATTR_NUM_COLS, err);
// 读取结果
while (OCIStmtFetch2(stmt, err, 1, OCI_FETCH_NEXT, 0, OCI_DEFAULT) == OCI_SUCCESS) {
for (int i = 1; i <= numCols; i++) {
char colVal[256];
ub2 colLen;
OCIVar *var;
OCIDefine *defn;
OCIDefineByPos(stmt, &defn, err, i, (void *)colVal, sizeof(colVal), SQLT_STR,
(void *)&colLen, (void *)NULL, OCI_DEFAULT);
OCIStmtGetPieceInfo(stmt, err, (dvoid **)&var, (ub4 *)&colLen, (ub1 *)&colVal, NULL, 0, OCI_ATTR_INPUT);
OCIStmtFetch2(stmt, err, (ub4)1, OCI_FETCH_NEXT, 0, OCI_DEFAULT);
printf("%s\t", strip_whitespace(colVal));
}
printf("\n");
}
// 释放OCI资源
OCISessionEnd(env, err, session, OCI_DEFAULT);
OCIServerDetach(server, err, OCI_DEFAULT);
OCIHandleFree(server, OCI_HTYPE_SERVER);
OCIHandleFree(session, OCI_HTYPE_SESSION);
OCIHandleFree(stmt, OCI_HTYPE_STMT);
OCIHandleFree(err, OCI_HTYPE_ERROR);
OCIHandleFree(env, OCI_HTYPE_ENV);
return 0;
}
```
需要替换 `<数据库地址>`, `<用户名>`, `<密码>`, `<表名>` 为实际的数据库地址、用户名、密码和要操作的表名。此代码实现了连接到Oracle数据库,并执行了一个简单的查询操作,将结果打印出来。可以根据具体需求进行进一步的CRUD操作的编写。
阅读全文