C连接Oracle的demo
时间: 2024-05-02 21:18:02 浏览: 163
下面是一个C语言连接Oracle数据库的简单示例:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <oci.h>
#define MAX_COL_LENGTH 256
int main()
{
OCIEnv *envhp;
OCIError *errhp;
OCISvcCtx *svchp;
OCIStmt *stmthp;
OCIDefine *defnp;
OCIDateTime *datehp;
OCIBind *bindhp;
OCILobLocator *lobhp;
OCINumber *numhp;
OCITrans *transhp;
OCIServer *srvhp;
char *username = "your_username";
char *password = "your_password";
char *dbname = "your_database_name";
char *query = "SELECT * FROM your_table";
sword status;
int col_count, i, j;
char col_name[MAX_COL_LENGTH];
char col_value[MAX_COL_LENGTH];
ub2 col_type;
ub4 col_length;
/* 初始化OCI环境 */
OCIEnvCreate(&envhp, OCI_DEFAULT, 0, 0, 0, 0, 0, 0);
OCIHandleAlloc(envhp, (void **)&errhp, OCI_HTYPE_ERROR, 0, 0);
OCIHandleAlloc(envhp, (void **)&srvhp, OCI_HTYPE_SERVER, 0, 0);
OCIHandleAlloc(envhp, (void **)&svchp, OCI_HTYPE_SVCCTX, 0, 0);
OCIHandleAlloc(envhp, (void **)&stmthp, OCI_HTYPE_STMT, 0, 0);
OCIHandleAlloc(envhp, (void **)&transhp, OCI_HTYPE_TRANS, 0, 0);
/* 连接Oracle数据库 */
OCIServerAttach(srvhp, errhp, (text *)dbname, strlen(dbname), OCI_DEFAULT);
OCIAttrSet(svchp, OCI_HTYPE_SVCCTX, srvhp, 0, OCI_ATTR_SERVER, errhp);
OCIHandleAlloc(envhp, (void **)&bindhp, OCI_HTYPE_BIND, 0, 0);
OCIHandleAlloc(envhp, (void **)&defnp, OCI_HTYPE_DEFINE, 0, 0);
OCIAttrSet((void *)stmthp, OCI_HTYPE_STMT, (void *)svchp, 0, OCI_ATTR_SVCCTX, errhp);
OCILogon2(envhp, errhp, &svchp, (text *)username, strlen(username), (text *)password, strlen(password), (text *)dbname, strlen(dbname), OCI_DEFAULT);
/* 开始事务 */
OCIAttrSet((void *)svchp, OCI_HTYPE_SVCCTX, (void *)transhp, 0, OCI_ATTR_TRANS, errhp);
OCITransStart(svchp, errhp, 2, OCI_TRANS_NEW);
/* 执行SQL查询 */
OCIStmtPrepare(stmthp, errhp, (text *)query, strlen(query), OCI_NTV_SYNTAX, OCI_DEFAULT);
OCIStmtExecute(svchp, stmthp, errhp, 0, 0, 0, 0, OCI_DEFAULT);
/* 获取查询结果列数 */
OCIAttrGet(stmthp, OCI_HTYPE_STMT, &col_count, 0, OCI_ATTR_PARAM_COUNT, errhp);
/* 输出查询结果 */
for (i = 1; i <= col_count; i++) {
OCIParamGet(stmthp, OCI_HTYPE_STMT, errhp, (void **)&defnp, i);
OCIAttrGet(defnp, OCI_DTYPE_PARAM, &col_name, 0, OCI_ATTR_NAME, errhp);
OCIAttrGet(defnp, OCI_DTYPE_PARAM, &col_type, 0, OCI_ATTR_DATA_TYPE, errhp);
OCIAttrGet(defnp, OCI_DTYPE_PARAM, &col_length, 0, OCI_ATTR_DATA_SIZE, errhp);
printf("%s\t", col_name);
}
printf("\n");
while ((status = OCIStmtFetch(stmthp, errhp, 1, OCI_FETCH_NEXT, OCI_DEFAULT)) == OCI_SUCCESS) {
for (i = 1; i <= col_count; i++) {
OCIParamGet(stmthp, OCI_HTYPE_STMT, errhp, (void **)&defnp, i);
OCIAttrGet(defnp, OCI_DTYPE_PARAM, &col_type, 0, OCI_ATTR_DATA_TYPE, errhp);
OCIAttrGet(defnp, OCI_DTYPE_PARAM, &col_length, 0, OCI_ATTR_DATA_SIZE, errhp);
switch (col_type) {
case SQLT_INT:
OCIDefineByPos(stmthp, &defnp, errhp, i, (void *)&j, sizeof(j), SQLT_INT, 0, 0, 0, OCI_DEFAULT);
printf("%d\t", j);
break;
case SQLT_CHR:
OCIDefineByPos(stmthp, &defnp, errhp, i, (void *)col_value, col_length, SQLT_STR, 0, 0, 0, OCI_DEFAULT);
printf("%s\t", col_value);
break;
case SQLT_NUM:
OCIDefineByPos(stmthp, &defnp, errhp, i, (void *)&numhp, sizeof(numhp), SQLT_NUM, 0, 0, 0, OCI_DEFAULT);
OCINumberToString(envhp, numhp, (text *)"999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999", 99, (text *)col_value, MAX_COL_LENGTH, 0);
printf("%s\t", col_value);
break;
case SQLT_DAT:
OCIDefineByPos(stmthp, &defnp, errhp, i, (void *)&datehp, sizeof(datehp), SQLT_DAT, 0, 0, 0, OCI_DEFAULT);
OCIDateTimeToText(envhp, errhp, datehp, (text *)"YYYY-MM-DD HH24:MI:SS", 19, (text *)col_value, MAX_COL_LENGTH);
printf("%s\t", col_value);
break;
case SQLT_CLOB:
OCIDefineByPos(stmthp, &defnp, errhp, i, (void *)&lobhp, sizeof(lobhp), SQLT_CLOB, 0, 0, 0, OCI_DEFAULT);
OCILobRead(svchp, errhp, lobhp, &col_length, 1, (void *)col_value, col_length, 0, 0, 0, 0);
printf("%s\t", col_value);
break;
default:
printf("Unknown data type\n");
break;
}
}
printf("\n");
}
/* 提交事务 */
if (status != OCI_NO_DATA) {
printf("Error: %d\n", status);
}
OCITransCommit(svchp, errhp, OCI_DEFAULT);
/* 断开连接,释放资源 */
OCILogoff(svchp, errhp);
OCIHandleFree(srvhp, OCI_HTYPE_SERVER);
OCIHandleFree(svchp, OCI_HTYPE_SVCCTX);
OCIHandleFree(stmthp, OCI_HTYPE_STMT);
OCIHandleFree(transhp, OCI_HTYPE_TRANS);
OCIHandleFree(envhp, OCI_HTYPE_ENV);
return 0;
}
```
该示例包含了连接数据库、执行查询、输出结果以及提交事务的基本步骤。具体实现方式可能会因Oracle版本不同而有所差异,请根据实际情况进行调整。
阅读全文