C Oracle Guide是一款在C语言中操作Oracle数据库的指南,可以帮助开发者更高效地使用Oracle数据库。下面将详细介绍其使用方法和注意事项。
一、连接Oracle数据库
#include#include#include#includeint main(void)
{
OCIEnv *envhp;
OCIError *errhp;
OCIStmt *stmthp;
OCISession *authp;
OCIServer *svrhp;
sword status;
OCIInitialize((ub4) OCI_DEFAULT, (dvoid *)0,
(dvoid * (*)(dvoid *, size_t)) 0,
(dvoid * (*)(dvoid *, dvoid *, size_t)) 0,
(void (*)(dvoid *, dvoid *)) 0 );
OCIEnvInit(&envhp, (ub4) OCI_DEFAULT, (size_t) 0,
(dvoid **) 0);
OCIHandleAlloc((dvoid *) envhp, (dvoid **) &errhp, (ub4) OCI_HTYPE_ERROR,
(size_t) 0, (dvoid **) 0);
OCIHandleAlloc((dvoid *) envhp, (dvoid **) &svrhp, (ub4) OCI_HTYPE_SERVER,
(size_t) 0, (dvoid **) 0);
status = OCIServerAttach(svrhp, errhp, (text *)"ORCL", (sb4) strlen("ORCL"),
(ub4) OCI_DEFAULT);
if (status != OCI_SUCCESS)
{
OCIHandleFree((dvoid *) errhp, (ub4) OCI_HTYPE_ERROR);
OCIHandleFree((dvoid *) svrhp, (ub4) OCI_HTYPE_SERVER);
OCIHandleFree((dvoid *) envhp, (ub4) OCI_HTYPE_ENV);
exit(EXIT_FAILURE);
}
printf("Connected to Oracle database\n");
OCISessionBegin(envhp, errhp, authp, (ub4) OCI_CRED_RDBMS,
(ub4) OCI_DEFAULT);
OCIHandleFree((dvoid *) errhp, (ub4) OCI_HTYPE_ERROR);
OCIHandleFree((dvoid *) svrhp, (ub4) OCI_HTYPE_SERVER);
OCIHandleFree((dvoid *) envhp, (ub4) OCI_HTYPE_ENV);
return 0;
}
上面的代码中,通过OCIInitialize初始化环境变量并申请句柄,然后通过OCIServerAttach连接到ORCL数据库,最后通过OCISessionBegin开始会话,即成功连接Oracle数据库。
二、执行SQL语句
int executeSQL(const char *sql)
{
OCIEnv *envhp;
OCIError *errhp;
OCIStmt *stmthp;
OCISession *authp;
OCIServer *svrhp;
OCIDefine *defnp;
ub4 nrows = 0;
ub2 col_type;
char col_name[1024];
ub2 col_name_length;
sword status;
OCIInitialize((ub4) OCI_DEFAULT, (dvoid *)0,
(dvoid * (*)(dvoid *, size_t)) 0,
(dvoid * (*)(dvoid *, dvoid *, size_t)) 0,
(void (*)(dvoid *, dvoid *)) 0 );
OCIEnvInit(&envhp, (ub4) OCI_DEFAULT, (size_t) 0,
(dvoid **) 0);
OCIHandleAlloc((dvoid *) envhp, (dvoid **) &errhp, (ub4) OCI_HTYPE_ERROR,
(size_t) 0, (dvoid **) 0);
OCIHandleAlloc((dvoid *) envhp, (dvoid **) &svrhp, (ub4) OCI_HTYPE_SERVER,
(size_t) 0, (dvoid **) 0);
status = OCIServerAttach(svrhp, errhp, (text *)"ORCL", (sb4) strlen("ORCL"),
(ub4) OCI_DEFAULT);
if (status != OCI_SUCCESS)
{
OCIHandleFree((dvoid *) errhp, (ub4) OCI_HTYPE_ERROR);
OCIHandleFree((dvoid *) svrhp, (ub4) OCI_HTYPE_SERVER);
OCIHandleFree((dvoid *) envhp, (ub4) OCI_HTYPE_ENV);
return 1;
}
OCIHandleAlloc((dvoid *) envhp, (dvoid **) &stmthp, (ub4) OCI_HTYPE_STMT,
(size_t) 0, (dvoid **) 0);
OCIStmtPrepare(stmthp, errhp, (text *) sql, (ub4) strlen(sql), (ub4) OCI_NTV_SYNTAX, (ub4) OCI_DEFAULT);
OCIStmtExecute(svrhp, stmthp, errhp, (ub4) 1, (ub4) 0, (OCISnapshot *) NULL, (OCISnapshot *) NULL,OCI_DEFAULT);
OCIDefineByPos(stmthp, &defnp, errhp, 1, (dvoid *) col_name, (sb4) sizeof(col_name), SQLT_CHR, (dvoid *) 0, (ub2 *) &col_name_length, (ub2 *) 0, OCI_DEFAULT);
col_type = SQLT_STR;
for (;;) {
int row_status = OCIStmtFetch(stmthp, errhp, 1, OCI_FETCH_NEXT, OCI_DEFAULT);
if (row_status == OCI_NO_DATA)
break;
char col_value[1024];
ub2 col_value_length;
memset(col_value, 0, sizeof(col_value));
OCIDefineByPos(stmthp, &defnp, errhp, 1, (dvoid *) col_value, (sb4) sizeof(col_value), col_type, (dvoid *) 0, (ub2 *) &col_value_length, (ub2 *) 0, OCI_DEFAULT);
printf("%s = %s\n", col_name, col_value);
nrows++;
}
OCIHandleFree((dvoid *) errhp, (ub4) OCI_HTYPE_ERROR);
OCIHandleFree((dvoid *) svrhp, (ub4) OCI_HTYPE_SERVER);
OCIHandleFree((dvoid *) stmthp, (ub4) OCI_HTYPE_STMT);
OCIHandleFree((dvoid *) envhp, (ub4) OCI_HTYPE_ENV);
return nrows;
}
上面的代码中,通过OCIStmtPrepare预编译SQL,并通过OCIStmtExecute执行SQL语句,然后通过OCIStmtFetch获取结果集,并通过OCIDefineByPos定义变量和变量类型,最后输出结果。
三、注意事项
1.在编译时需要指定头文件和库文件路径,例如:
gcc -I$ORACLE_HOME/sdk/include -L$ORACLE_HOME/sdk/lib -lclntsh program.c -o program
2.在使用字符串类型时,需要将字符串长度减去1,例如:
char name[20] = "Tom";
//传入的参数需要将字符串长度减去1
executeSQL("select * from people where name = 'Tom'");
总之,C Oracle Guide是一款非常实用的指南,在操作Oracle数据库时可以提高开发效率和减少出错率。