淘先锋技术网

首页 1 2 3 4 5 6 7

在数据库开发中,我们经常需要查询数据库中的数据,而C程序对于查询Oracle数据库的支持,CRT就是其中一种常用的方法。CRT是Oracle提供的一个用于数据库访问的库,可以在C语言中操作Oracle数据库。

举个例子,我们需要查询学生成绩表中所有成绩大于等于90分的学生记录:

SELECT *
FROM score
WHERE grade >= 90;

如果要在C语言中使用CRT查询,需要先连接数据库:

#include <stdio.h>
#include <stdlib.h>
#include <oci.h>
int main() {
OCIEnv *envhp;
OCIError *errhp;
OCISvcCtx *svchp;
OCIStmt *stmthp;
OCIDefine *defhp = NULL;
OCIDateTime *dthp = NULL;
OCILobLocator *lobhp = NULL;
OCIInitialize((ub4) OCI_OBJECT, (dvoid *)0,
(dvoid * (*)(dvoid *, size_t)) 0,
(dvoid * (*)(dvoid *, dvoid *, size_t))0,
(void (*)(dvoid *, dvoid *)) 0 );
OCIHandleAlloc( (dvoid *) 0, (dvoid **) &envhp, (ub4) OCI_HTYPE_ENV,
0, (dvoid **) 0 );
OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &errhp, (ub4) OCI_HTYPE_ERROR,
0, (dvoid **) 0 );
OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &svchp, (ub4) OCI_HTYPE_SVCCTX,
0, (dvoid **) 0 );
OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &stmthp, (ub4) OCI_HTYPE_STMT,
0, (dvoid **) 0 );
OCILogon(envhp, errhp, &svchp, (OraText *) "username", strlen("username"),
(OraText *) "password", strlen("password"), (OraText *) "dbname", strlen("dbname"));
OCIStmtPrepare(stmthp, errhp, (OraText *) "SELECT * FROM score WHERE grade >= 90", strlen("SELECT * FROM score WHERE grade >= 90"),
(ub4) OCI_NTV_SYNTAX, (ub4) OCI_DEFAULT);
OCIStmtExecute(svchp, stmthp, errhp, (ub4) 0, (ub4) 0, (CONST OCISnapshot *) NULL, (OCISnapshot *) NULL, OCI_DEFAULT);
return 0;
}

在连接完成之后,我们就可以使用OCIStmtPrepare函数准备查询语句,使用OCIStmtExecute函数执行查询语句了。如果查询结果比较多,我们可以使用OCIDefine函数绑定查询结果。

举个例子,我们需要查询学生成绩表中的学生姓名和成绩:

SELECT name, grade
FROM score;

在C语言中,我们可以使用OCIDefine函数将查询结果中的每个字段绑定到相应的变量中:

#include <stdio.h>
#include <stdlib.h>
#include <oci.h>
int main() {
OCIEnv *envhp;
OCIError *errhp;
OCISvcCtx *svchp;
OCIStmt *stmthp;
OCIDefine *name_defhp = NULL;
OCIDefine *grade_defhp = NULL;
text name[32];
int grade;
OCIInitialize((ub4) OCI_OBJECT, (dvoid *)0,
(dvoid * (*)(dvoid *, size_t)) 0,
(dvoid * (*)(dvoid *, dvoid *, size_t))0,
(void (*)(dvoid *, dvoid *)) 0 );
OCIHandleAlloc( (dvoid *) 0, (dvoid **) &envhp, (ub4) OCI_HTYPE_ENV,
0, (dvoid **) 0 );
OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &errhp, (ub4) OCI_HTYPE_ERROR,
0, (dvoid **) 0 );
OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &svchp, (ub4) OCI_HTYPE_SVCCTX,
0, (dvoid **) 0 );
OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &stmthp, (ub4) OCI_HTYPE_STMT,
0, (dvoid **) 0 );
OCILogon(envhp, errhp, &svchp, (OraText *) "username", strlen("username"),
(OraText *) "password", strlen("password"), (OraText *) "dbname", strlen("dbname"));
OCIStmtPrepare(stmthp, errhp, (OraText *) "SELECT name, grade FROM score", strlen("SELECT name, grade FROM score"),
(ub4) OCI_NTV_SYNTAX, (ub4) OCI_DEFAULT);
OCIHandleAlloc(envhp, (dvoid **)&name_defhp, (ub4) OCI_HTYPE_DEFINE, (CONST size_t *) sizeof(text),
(dvoid **) &name);
OCIHandleAlloc(envhp, (dvoid **)&grade_defhp, (ub4) OCI_HTYPE_DEFINE, (CONST size_t *) sizeof(int),
(dvoid **) &grade);
OCIDefineByPos(stmthp, &name_defhp, errhp, (ub4) 1, (dvoid *) name, (ub4) 32, SQLT_STR,
(dvoid *) 0, (ub2 *) 0, (ub2 *) 0, (ub4) OCI_DEFAULT);
OCIDefineByPos(stmthp, &grade_defhp, errhp, (ub4) 2, (dvoid *) &grade, (ub4) sizeof(int), SQLT_INT,
(dvoid *) 0, (ub2 *) 0, (ub2 *) 0, (ub4) OCI_DEFAULT);
OCIStmtExecute(svchp, stmthp, errhp, (ub4) 0, (ub4) 0, (CONST OCISnapshot *) NULL, (OCISnapshot *) NULL, OCI_DEFAULT);
printf("name: %s, grade: %d\n", name, grade);
return 0;
}

以上便是使用CRT查询Oracle数据库的一些基础知识。除了以上提到的函数外,Oracle CRT还有很多函数和数据结构,需要进一步学习和了解。