句柄和描述符属性管理
OCI可以通过调用OCIDescribeAny,OCIAttrGet,OCIAttrSet,OCIParamGet接口对句柄和描述符属性资源进行管理。
句柄属性管理
在分配完环境句柄后,可以调用OCIAttrSet和OCIAttrGet对句柄属性进行设置和获取。
oratext phost[10] = {0};
sword r2 = OCIAttrGet(envhp, OCI_HTYPE_ENV, phost, &plen, OCI_ATTR_LDAP_HOST, errhp);
printf("OCI_ATTR_LDAP_HOST=%s,res=%s\n", phost,getErrStr(r2));
sword r3 = OCIAttrGet(stmthp, OCI_HTYPE_STMT, &uvalue, &plen, OCI_ATTR_BIND_COUNT, errhp);
printf("OCI_ATTR_BIND_COUNT=%d,res=%s\n", uvalue,getErrStr(r3));
OCIEnv *envhpnew = NULL;
sword r4 = OCIAttrGet(stmthp, OCI_HTYPE_STMT, &envhpnew, &plen, OCI_ATTR_ENV, errhp);
printf("OCI_ATTR_ENV,res=%s\n", getErrStr(r4));
boolean bl;
sword r5 = OCIAttrGet(stmthp, OCI_HTYPE_STMT, &bl, &plen, OCI_ATTR_FETCH_ROWID, errhp);
printf("OCI_ATTR_FETCH_ROWID=%d,res=%s \n", bl,getErrStr(r5));
uvalue = 0;
sword r6 = OCIAttrGet(stmthp, OCI_HTYPE_STMT, &uvalue, &plen, OCI_ATTR_STMT_STATE, errhp);
printf("OCI_ATTR_STMT_STATE=%d,res=%s\n", uvalue,getErrStr(r6));
uvalue = 0;
sword r7 = OCIAttrGet(stmthp, OCI_HTYPE_STMT, &uvalue, &plen, OCI_ATTR_PARAM_COUNT, errhp);
printf("OCI_ATTR_PARAM_COUNT=%d,res=%s\n", uvalue,getErrStr(r7));
sword status = 0;
static OCIServer *svrhp = (OCIServer *)0;
OCIHandleAlloc(envhp1, (dvoid **)&svrhp, OCI_HTYPE_SERVER, (size_t)0,(dvoid **)0);
status = OCIServerAttach(svrhp, errhp, (text *)dsn, (sb4)strlen((char *)dsn),OCI_DEFAULT);
OCIHandleAlloc(envhp1,(dvoid **)&svchp,OCI_HTYPE_SVCCTX,(size_t)0,(dvoid **)0);
sword rc = OCIAttrSet(svchp, OCI_HTYPE_SVCCTX, (dvoid *)svrhp, (ub4)0,OCI_ATTR_SERVER, errhp);
printf("设置OCI_ATTR_SERVER的结果为:%s\n",getErrStr(rc));
static OCISession *sesnhp = (OCISession *)0;
OCIHandleAlloc(envhp1, (dvoid **)&sesnhp, OCI_HTYPE_SESSION, (size_t)0, (dvoid **)0);
rc = OCIAttrSet(sesnhp, OCI_HTYPE_SESSION, (dvoid *)username, (ub4)strlen((char *)username), OCI_ATTR_USERNAME, errhp);
printf("设置OCI_ATTR_USERNAME的结果为:%s\n", getErrStr(rc));
rc = OCIAttrSet(sesnhp, OCI_HTYPE_SESSION, (dvoid*)password, (ub4)strlen((char *)password), OCI_ATTR_PASSWORD, errhp);
printf("设置OCI_ATTR_PASSWORD的结果为:%s\n", getErrStr(rc));
status = OCISessionBegin(svchp,errhp,sesnhp,OCI_CRED_RDBMS,OCI_DEFAULT);
描述符属性管理
描述符可以通过调用OCIDescribeAny和OCIParamGet接口来获取属性。
if(OCIDescribeAny(svchp, errhp, "t2", strlen("t2"), OCI_OTYPE_NAME, 1, OCI_PTYPE_TABLE, (OCIDescribe *)dschp))
{
printf("FAILED: log_on()\n");
report_error(errhp);
return OCI_ERROR;
}
OCIParam *parmh = (OCIParam *) 0;
if (OCIAttrGet((void *)dschp, OCI_HTYPE_DESCRIBE, (void *)&parmh, (ub4 *)0,
OCI_ATTR_PARAM, errhp))
return OCI_ERROR;
ub1 param_type;
if (OCIAttrGet((void *)parmh, OCI_DTYPE_PARAM, (void *)¶m_type, (ub4 *)0,
OCI_ATTR_PTYPE, errhp))
return OCI_ERROR;
printf("OCI_ATTR_PTYPE=%d %d\n", param_type, OCI_PTYPE_TABLE);
ub2 numcols = 0;
if (OCIAttrGet((void *)parmh, OCI_DTYPE_PARAM, (void *)&numcols, (ub4 *)0,
OCI_ATTR_NUM_COLS, errhp))
return OCI_ERROR;
OCIParam *collsthd = (OCIParam *) 0;
if (OCIAttrGet((void *)parmh, OCI_DTYPE_PARAM, (void *)&collsthd, (ub4 *)0,
OCI_ATTR_LIST_COLUMNS, errhp)==OCI_NO_DATA)
return OCI_ERROR;
if (OCIAttrGet((void *)collsthd, OCI_DTYPE_PARAM, (void *)¶m_type, (ub4 *)0,
OCI_ATTR_PTYPE, errhp))
return OCI_ERROR;
printf("OCI_ATTR_PTYPE=%d %d\n", param_type, OCI_PTYPE_LIST);
for(int i=0;i<numcols;++i)
{
printf("\n第 %d 列描述符信息如下: \n", i+1);
if (OCIParamGet((void *)collsthd, OCI_DTYPE_PARAM, errhp, (void **)&colhd, (ub4)i+1))
return OCI_ERROR;
OraText *namep;
ub4 sizep =0;
OCIAttrGet((dvoid*) colhd, (ub4) OCI_DTYPE_PARAM, (dvoid**) &namep, (ub4 *) &sizep, (ub4)OCI_ATTR_NAME, (OCIError *)errhp);
printf("OCI_ATTR_NAME=%s, %d\n", namep, sizep);
if (OCIAttrGet((void *)colhd, OCI_DTYPE_PARAM, (void *)¶m_type, (ub4 *)0,
OCI_ATTR_PTYPE, errhp))
return OCI_ERROR;
printf("OCI_ATTR_PTYPE=%d %d\n", param_type, OCI_PTYPE_COL);
OCIAttrGet((dvoid*) colhd, (ub4) OCI_DTYPE_PARAM, (dvoid**) &namep, (ub4 *) &sizep, (ub4)OCI_ATTR_SCHEMA_NAME, (OCIError *)errhp);
printf("OCI_ATTR_SCHEMA_NAME=%s, %d\n", namep, sizep);
ub2 dtype;
OCIAttrGet((dvoid*) colhd, (ub4) OCI_DTYPE_PARAM, (dvoid*) &dtype, (ub4 *) &sizep, (ub4)OCI_ATTR_DATA_TYPE, (OCIError *)errhp);
printf("OCI_ATTR_DATA_TYPE=%d, %d\n", dtype, sizep);
OCIAttrGet((dvoid*) colhd, (ub4) OCI_DTYPE_PARAM, (dvoid*) &dtype, (ub4 *) &sizep, (ub4)OCI_ATTR_DATA_SIZE, (OCIError *)errhp);
printf("OCI_ATTR_DATA_SIZE=%d, %d\n", dtype, sizep);
OCIAttrGet((dvoid*) colhd, (ub4) OCI_DTYPE_PARAM, (dvoid **) &namep, (ub4 *) &sizep, (ub4) OCI_ATTR_TYPE_NAME, (OCIError *)errhp);
printf("OCI_ATTR_TYPE_NAME=%s,%d\n", namep, sizep);
OCIAttrGet((dvoid*) colhd, (ub4) OCI_DTYPE_PARAM, (dvoid **) &dtype, (ub4 *) &sizep, (ub4)OCI_ATTR_PRECISION, (OCIError *)errhp);
printf("OCI_ATTR_PRECISION=%d, %d\n", dtype, sizep);
OCIAttrGet((dvoid*) colhd, (ub4) OCI_DTYPE_PARAM, (dvoid **) &dtype, (ub4 *) &sizep, (ub4)OCI_ATTR_SCALE, (OCIError *)errhp);
printf("OCI_ATTR_SCALE=%d, %d\n", dtype, sizep);
//if(colhd)
// OCIDescriptorFree(colhd, (ub4)OCI_DTYPE_PARAM);
}