我的问题找不到任何好的答案。我必须编写程序(用C语言编写),它从配置文件中读取SQL查询,执行它并收集返回的数据。当我的应用程序启动时,查询结果中没有关于列的信息。我应该从数据库中获取它。 我的代码几行:
OCIStmtPrepare(statementHandler, errhp, statement, (ub4) strlen((char *) statement), (ub4);
OCIStmtExecute(svchp, statementHandler, errhp, (ub4) 1, (ub4) 0,(const OCISnapshot *) NULL, (OCISnapshot *) NULL, (ub4) OCI_DEFAULT);
ub4 parmcnt = 0;//columns count
OCIAttrGet ((dvoid *)statementHandler, (ub4)OCI_HTYPE_STMT, (dvoid *) &parmcnt, (ub4 *) 0, (ub4)OCI_ATTR_PARAM_COUNT, errhp);\
现在我只有列数了。是否有一些函数可以检索列数据类型?
答案 0 :(得分:2)
有OCI_ATTR_DATA_TYPE
。使用OCI_ATTR_PARAM_COUNT
获取列数的方式,您可以使用OCI_ATTR_DATA_TYPE
来获取列类型。例如,以下是我之前用过类似代码的一些示例代码(注意它不会与您的100%匹配,但您应该明白这一点) -
checkerr(errhp, OCIAttrGet((CONST dvoid *)stmthp, (ub4)OCI_HTYPE_STMT,
(void *)¶mcnt, (ub4 *)0,
(ub4) OCI_ATTR_PARAM_COUNT, errhp));
fprintf(stdout, "Number of columns in query is %d\n",paramcnt);
for (i=1; i<=paramcnt; i++)
{
checkerr(errhp, OCIParamGet((dvoid *)stmthp, (ub4)OCI_HTYPE_STMT,
(OCIError *)errhp, (dvoid **)¶mhp,
(ub4)i));
checkerr(errhp, OCIAttrGet((dvoid *)paramhp,
(ub4)OCI_DTYPE_PARAM, (dvoid *)&name,(ub4 *)&namelen,
(ub4) OCI_ATTR_NAME, errhp));
checkerr(errhp, OCIAttrGet((dvoid *)paramhp,
(ub4)OCI_DTYPE_PARAM, (dvoid *)&type,(ub4 *)0,
(ub4) OCI_ATTR_DATA_TYPE, errhp));
checkerr(errhp, OCIAttrGet((dvoid *)paramhp,
(ub4)OCI_DTYPE_PARAM, (dvoid *)&size,(ub4 *)0,
(ub4) OCI_ATTR_DATA_SIZE, errhp));
fprintf(stdout, "Column %d: name = %.*s, type = %d, size = %d, \n", i, namelen, name, type, size);
}