gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
6.2KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>33.5. Retrieving Query Results Row-by-Row</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="libpq-async.html" title="33.4. Asynchronous Command Processing" /><link rel="next" href="libpq-cancel.html" title="33.6. Canceling Queries in Progress" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">33.5. Retrieving Query Results Row-by-Row</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="libpq-async.html" title="33.4. Asynchronous Command Processing">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="libpq.html" title="Chapter 33. libpq - C Library">Up</a></td><th width="60%" align="center">Chapter 33. <span xmlns="http://www.w3.org/1999/xhtml" class="application">libpq</span> - C Library</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="libpq-cancel.html" title="33.6. Canceling Queries in Progress">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="LIBPQ-SINGLE-ROW-MODE"><div class="titlepage"><div><div><h2 class="title" style="clear: both">33.5. Retrieving Query Results Row-by-Row</h2></div></div></div><a id="id-1.7.3.12.2" class="indexterm"></a><p>
  3. Ordinarily, <span class="application">libpq</span> collects a SQL command's
  4. entire result and returns it to the application as a single
  5. <code class="structname">PGresult</code>. This can be unworkable for commands
  6. that return a large number of rows. For such cases, applications can use
  7. <code class="function">PQsendQuery</code> and <code class="function">PQgetResult</code> in
  8. <em class="firstterm">single-row mode</em>. In this mode, the result row(s) are
  9. returned to the application one at a time, as they are received from the
  10. server.
  11. </p><p>
  12. To enter single-row mode, call <code class="function">PQsetSingleRowMode</code>
  13. immediately after a successful call of <code class="function">PQsendQuery</code>
  14. (or a sibling function). This mode selection is effective only for the
  15. currently executing query. Then call <code class="function">PQgetResult</code>
  16. repeatedly, until it returns null, as documented in <a class="xref" href="libpq-async.html" title="33.4. Asynchronous Command Processing">Section 33.4</a>. If the query returns any rows, they are returned
  17. as individual <code class="structname">PGresult</code> objects, which look like
  18. normal query results except for having status code
  19. <code class="literal">PGRES_SINGLE_TUPLE</code> instead of
  20. <code class="literal">PGRES_TUPLES_OK</code>. After the last row, or immediately if
  21. the query returns zero rows, a zero-row object with status
  22. <code class="literal">PGRES_TUPLES_OK</code> is returned; this is the signal that no
  23. more rows will arrive. (But note that it is still necessary to continue
  24. calling <code class="function">PQgetResult</code> until it returns null.) All of
  25. these <code class="structname">PGresult</code> objects will contain the same row
  26. description data (column names, types, etc) that an ordinary
  27. <code class="structname">PGresult</code> object for the query would have.
  28. Each object should be freed with <code class="function">PQclear</code> as usual.
  29. </p><p>
  30. </p><div class="variablelist"><dl class="variablelist"><dt id="LIBPQ-PQSETSINGLEROWMODE"><span class="term">
  31. <code class="function">PQsetSingleRowMode</code>
  32. <a id="id-1.7.3.12.5.1.1.1.2" class="indexterm"></a>
  33. </span></dt><dd><p>
  34. Select single-row mode for the currently-executing query.
  35. </p><pre class="synopsis">
  36. int PQsetSingleRowMode(PGconn *conn);
  37. </pre><p>
  38. </p><p>
  39. This function can only be called immediately after
  40. <code class="function">PQsendQuery</code> or one of its sibling functions,
  41. before any other operation on the connection such as
  42. <code class="function">PQconsumeInput</code> or
  43. <code class="function">PQgetResult</code>. If called at the correct time,
  44. the function activates single-row mode for the current query and
  45. returns 1. Otherwise the mode stays unchanged and the function
  46. returns 0. In any case, the mode reverts to normal after
  47. completion of the current query.
  48. </p></dd></dl></div><p>
  49. </p><div class="caution"><h3 class="title">Caution</h3><p>
  50. While processing a query, the server may return some rows and then
  51. encounter an error, causing the query to be aborted. Ordinarily,
  52. <span class="application">libpq</span> discards any such rows and reports only the
  53. error. But in single-row mode, those rows will have already been
  54. returned to the application. Hence, the application will see some
  55. <code class="literal">PGRES_SINGLE_TUPLE</code> <code class="structname">PGresult</code>
  56. objects followed by a <code class="literal">PGRES_FATAL_ERROR</code> object. For
  57. proper transactional behavior, the application must be designed to
  58. discard or undo whatever has been done with the previously-processed
  59. rows, if the query ultimately fails.
  60. </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="libpq-async.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="libpq.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="libpq-cancel.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">33.4. Asynchronous Command Processing </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 33.6. Canceling Queries in Progress</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1