gpt4 book ai didi

sql - Postgres Async API 检测查询结束

转载 作者:太空狗 更新时间:2023-10-29 17:24:25 28 4
gpt4 key购买 nike

我正在使用 PostgreSQL C API。阅读 documentation它声明当 PQgetResult 返回 NULL 时查询完成,如果 PQisBusy 不返回 0,PQgetResult 将阻塞。但是,如果没有更多输入,PQisBusy 返回 1,所以我无法调用 PQgetResult 并获取 NULL。因此我不知道查询是否结束。有没有其他方法可以知道查询是否完成?我是否误解了异步 API?

----编辑------

作为C代码的基本思想是:

PQsendQuery(conn, query)

while(true)
{
PQconsumeInput(conn)
if(PQisBusy(conn) == 0)
{
PGresult* res = PQgetResult(conn);
if(res)
{
//print result
}
else
{
//res == NULL
//indicates query is over
break;
}
}
}

结果将被打印出来,但循环永远不会终止。因为 PQisBusy 只返回 0 一次。

最佳答案

我认为你需要的是 PQsetSingleRowModePQgetResult 每次返回一行。

来自 http://www.postgresql.org/docs/9.2/static/libpq-single-row-mode.html

Ordinarily, libpq collects a SQL command's entire result and returns it to the application as a single PGresult. This can be unworkable for commands that return a large number of rows. For such cases, applications can use PQsendQuery and PQgetResult in single-row mode. In this mode, the result row(s) are returned to the application one at a time, as they are received from the server.

If the query returns any rows, they are returned as individual PGresult objects, which look like normal query results except for having status code PGRES_SINGLE_TUPLE instead of PGRES_TUPLES_OK. After the last row, or immediately if the query returns zero rows, a zero-row object with status PGRES_TUPLES_OK is returned; this is the signal that no more rows will arrive.

查看示例代码: https://github.com/markokr/libpq-rowproc-demos/blob/master/demo-onerow-async.c

关于sql - Postgres Async API 检测查询结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13663151/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com