当使用 OCIStmtPrepare()
和 OCIBindByName()
时,有没有办法按名称进行绑定(bind),然后获取该绑定(bind)的位置作为 诠释
? OCIStmtGetBindInfo()
似乎没有这样做。谢谢!
最佳答案
似乎没有一种简单的方法可以做到这一点。我尝试使用未记录的(因为我在帮助文档中找不到它,但它在 oci.h header 中)OCI_ATTR_HANDLE_POSITION
在绑定(bind)上使用 OCIAttrGet()
句柄:
ub4 bpos = 0;
OCIBind *bindp;
OCIAttrGet(bindp, OCI_HTYPE_BIND, &bpos, 0, OCI_ATTR_HANDLE_POSITION, errhp);
不幸的是,这似乎适用于您按位置绑定(bind)的绑定(bind)句柄,但对于任何按名称绑定(bind)的句柄都返回 0。
所以看起来你必须使用 OCIStmtGetBindInfo()
调用让它填充一个绑定(bind)变量名称数组,然后遍历它以找到每个命名绑定(bind)的位置,方法是将绑定(bind)名称与绑定(bind)变量名称数组中的值进行比较(其值将大写):
sb4 found = 0;
text* bvns[100];
ub1 bvnls[100];
text* invs[100];
ub1 invls[100];
ub1 dupls[100];
OCIBind* bhnds[100];
OCIStmtGetBindInfo(stmthp, errhp, (ub4)100, (ub4)1, &found, bvns, bvnls, invs, invls, dupls, bhnds);
for (unsigned int col = 0; col < found; col++)
{
printf("%p is bound to name: %s", bhnds[col], bvns[col]);
}
关于语句中的多个占位符,需要注意的一件有趣的事情是,无论您的语句是否为匿名 block ,它的行为都会有所不同。即:
insert into foo (bar, baz) values (:bar, :bar)
将用 2 填充您的 found
输出变量(以及包含 2 个绑定(bind)信息的数组),而:
begin insert into foo (bar, baz) values (:bar, :bar); end;
将用 1 填充您的 found
输出变量(以及包含 1 个绑定(bind)信息的数组)。
尽管如此,在任何一种情况下,对 OCIBindByName()
的一次调用都会绑定(bind)两者。
关于c - 从 OCI 中的绑定(bind)名称获取绑定(bind)位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6236887/