当前位置: 技术问答>linux和unix
使用OCI编程,读取表的列数
来源: 互联网 发布时间:2015-09-27
本文导语: 最近在Unix上使用C对Oracle编程,可是我对Oracle不熟悉。现在遇到一个难题:怎样读取表中的列数。 如果有哪位大侠知道解决的方法,请回应。 先谢了。 | OCIEnv *penv; OCIError *perr; OCISvc...
最近在Unix上使用C对Oracle编程,可是我对Oracle不熟悉。现在遇到一个难题:怎样读取表中的列数。
如果有哪位大侠知道解决的方法,请回应。
先谢了。
如果有哪位大侠知道解决的方法,请回应。
先谢了。
|
OCIEnv *penv;
OCIError *perr;
OCISvcCtx *psvc;
int
describe_select_list(char *sql)
{
OCIStmt *pstmt;
OCIParam *pparam;
ub4 pos;
ub2 dtype;
ub2 dsize;
text *name;
ub4 name_length;
if (OCIHandleAlloc(penv, (dvoid **)&pstmt, OCI_HTYPE_STMT, 0, 0) == OCI_SUCCESS)
{
if (OCIStmtPrepare(pstmt, perr, (text *)sql, strlen(sql), OCI_NTV_SYNTAX, OCI_DEFAULT) == OCI_SUCCESS)
{
if (OCIStmtExecute(psvc, pstmt, perr, 0, 0, 0, 0, OCI_DEFAULT) == OCI_SUCCESS)
{
OCIAttrGet((dvoid *)pstmt, OCI_HTYPE_STMT, &name_length, 0, OCI_ATTR_PARAM_COUNT, perr);
printf("found %d total columns.n", name_length);
pos = 1;
while (OCIParamGet(pstmt, OCI_HTYPE_STMT, perr, (dvoid *)&pparam, pos) == OCI_SUCCESS)
{
OCIAttrGet((dvoid *)pparam, OCI_DTYPE_PARAM, (dvoid *)&dtype, 0, OCI_ATTR_DATA_TYPE, perr);
OCIAttrGet((dvoid *)pparam, OCI_DTYPE_PARAM, (dvoid *)&dsize, 0, OCI_ATTR_DATA_SIZE, perr);
OCIAttrGet((dvoid *)pparam, OCI_DTYPE_PARAM, (dvoid *)&name, &name_length, OCI_ATTR_NAME, perr);
printf("%02d> column = '%s', type = %d, size = %d.n", pos, name, dtype, dsize);
pos++;
}
OCIHandleFree(pstmt, OCI_HTYPE_STMT);
return 0;
}
}
}
OCIHandleFree(pstmt, OCI_HTYPE_STMT);
show_error_message("describe_select_list");
return -1;
}
OCIError *perr;
OCISvcCtx *psvc;
int
describe_select_list(char *sql)
{
OCIStmt *pstmt;
OCIParam *pparam;
ub4 pos;
ub2 dtype;
ub2 dsize;
text *name;
ub4 name_length;
if (OCIHandleAlloc(penv, (dvoid **)&pstmt, OCI_HTYPE_STMT, 0, 0) == OCI_SUCCESS)
{
if (OCIStmtPrepare(pstmt, perr, (text *)sql, strlen(sql), OCI_NTV_SYNTAX, OCI_DEFAULT) == OCI_SUCCESS)
{
if (OCIStmtExecute(psvc, pstmt, perr, 0, 0, 0, 0, OCI_DEFAULT) == OCI_SUCCESS)
{
OCIAttrGet((dvoid *)pstmt, OCI_HTYPE_STMT, &name_length, 0, OCI_ATTR_PARAM_COUNT, perr);
printf("found %d total columns.n", name_length);
pos = 1;
while (OCIParamGet(pstmt, OCI_HTYPE_STMT, perr, (dvoid *)&pparam, pos) == OCI_SUCCESS)
{
OCIAttrGet((dvoid *)pparam, OCI_DTYPE_PARAM, (dvoid *)&dtype, 0, OCI_ATTR_DATA_TYPE, perr);
OCIAttrGet((dvoid *)pparam, OCI_DTYPE_PARAM, (dvoid *)&dsize, 0, OCI_ATTR_DATA_SIZE, perr);
OCIAttrGet((dvoid *)pparam, OCI_DTYPE_PARAM, (dvoid *)&name, &name_length, OCI_ATTR_NAME, perr);
printf("%02d> column = '%s', type = %d, size = %d.n", pos, name, dtype, dsize);
pos++;
}
OCIHandleFree(pstmt, OCI_HTYPE_STMT);
return 0;
}
}
}
OCIHandleFree(pstmt, OCI_HTYPE_STMT);
show_error_message("describe_select_list");
return -1;
}
|
对Oracle的操作和对mysql的一样吗?