免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3697 | 回复: 4

oci中,如何在prepare的时候获取参数类型? [复制链接]

论坛徽章:
0
发表于 2007-07-07 19:29 |显示全部楼层
20可用积分
因为封装的需要,要求在prepare的时候,获取绑定参数的类型,例如 insert into table1(col1,col2) values(:c1, :c2),如何在prepare该sql语句的时候,获取col1和col2的类型?
谢谢~

论坛徽章:
0
发表于 2007-07-09 15:02 |显示全部楼层
select col1, col2 from table1;  //我不确定是否insert也可以,但是informix下,肯定是不可以的。还是用select吧
然后prepare这个语句,再describe
就能得到col1, col2的类型。
实际上 prepare,然后describe,是sql92标准定义的。不同的编程环境
提供了不同的实现,但是基本的思路都是一致的。

给你个例子

int i = 0;
ub4 numcols = 0;
ub2 type = 0;
OCIParam *colhd = (OCIParam *) 0;   /* column handle */

text *sqlstmt = (text *)"insert into table1(col1,col2) values(?,?)";  //试试这样?

checkerr(errhp, OCIStmtPrepare(stmthp, errhp, (OraText *)sqlstmt,
                    (ub4)strlen((char *)sqlstmt),
                    (ub4) OCI_NTV_SYNTAX, (ub4) OCI_DEFAULT)
);

/* initialize svchp, stmhp, errhp, rowoff, iters, snap_in, snap_out */
/* set the execution mode to OCI_DESCRIBE_ONLY. Note that setting the mode to
OCI_DEFAULT does an implicit describe of the statement in addition to executing
the statement */

checkerr(errhp, OCIStmtExecute(svchp, stmthp, errhp, 0, 0,
        (OCISnapshot *) 0, (OCISnapshot *) 0, OCI_DESCRIBE_ONLY)
);

/* Get the number of columns in the query */
checkerr(errhp, OCIAttrGet((dvoid *)stmthp, OCI_HTYPE_STMT, (dvoid *)&numcols,
                      (ub4 *)0, OCI_ATTR_PARAM_COUNT, errhp)
);

/* go through the column list and retrieve the datatype of each column. We
start from pos = 1 */
for (i = 1; i <= numcols; i++)
{
  /* get parameter for column i */
  checkerr(errhp, OCIParamGet((dvoid *)stmthp, OCI_HTYPE_STMT, errhp, (dvoid **)&colhd, i));

  /* get data-type of column i */
  type = 0;
  checkerr(errhp, OCIAttrGet((dvoid *)colhd, OCI_DTYPE_PARAM,
          (dvoid *)&type, (ub4 *)0, OCI_ATTR_DATA_TYPE, errhp)
);
}

[ 本帖最后由 ivhb 于 2007-7-10 09:17 编辑 ]

论坛徽章:
0
发表于 2007-07-09 23:42 |显示全部楼层

回复 #2 ivhb 的帖子

多谢。
不过,prepare的时候已经进行了硬解析了,应该可以获取各个参数的类型啊。

论坛徽章:
0
发表于 2007-07-10 09:17 |显示全部楼层
text *sqlstmt = (text *)"insert into table1(col1,col2) values(?,?)";  //试试这样?

论坛徽章:
0
发表于 2007-07-10 09:28 |显示全部楼层
原帖由 seer671 于 2007-7-7 19:29 发表
因为封装的需要,要求在prepare的时候,获取绑定参数的类型,例如 insert into table1(col1,col2) values(:c1, :c2),如何在prepare该sql语句的时候,获取col1和col2的类型?
谢谢~



你能可以直接prepare "insert into table1(col1,col2) values(:c1, :c2)"? 刚才我在EC上试了一下,
prepare "insert into table1(col1,col2) values(?, ?)", 然后describe,可以得到类型。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP