我必须为一个简单的查询编写 OCI 代码,例如 Select * from emp;
因为我不知道将返回多少行,所以我无法预分配缓冲区
因此,我在关联输出缓冲区时定义了一个 void **
指针。
这个想法是调用 OCIStmtExecute
和 iters = 0
since
For SELECT statements, if iters is nonzero, then defines must have been done for the statement handle. The execution fetches iters rows into these predefined buffers and prefetches more rows depending upon the prefetch row count. If you do not know how many rows the SELECT statement will retrieve, set iters to zero.
This function returns an error if iters=0 for non-SELECT statements.
之后我计划执行 OCIAttrGet(OCI_ATTR_ROW_COUNT)
以获取 select 语句返回的行数,然后为输出缓冲区分配内存,然后通过指定行数调用 OCIStmtFetch因此返回。
但是我得到的行数每次都是0,即使表有返回数据。
我的方法有什么缺陷吗?你能指出错误吗
最佳答案
您只需为一行分配空间,然后重复调用 OCIStmtFetch2()
直到到达结果集的末尾(这将给出错误代码 1403)。预取发生在 OCI 本身维护的缓冲区中,对您来说是透明的,因此这种方法并不低效。
关于c - OCI 调用 select 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17077526/