gooderp18绿色标准版
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

1164 lines
74KB

  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.3. Command Execution Functions</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-status.html" title="33.2. Connection Status Functions" /><link rel="next" href="libpq-async.html" title="33.4. Asynchronous Command Processing" /></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.3. Command Execution Functions</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="libpq-status.html" title="33.2. Connection Status Functions">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-async.html" title="33.4. Asynchronous Command Processing">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="LIBPQ-EXEC"><div class="titlepage"><div><div><h2 class="title" style="clear: both">33.3. Command Execution Functions</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="libpq-exec.html#LIBPQ-EXEC-MAIN">33.3.1. Main Functions</a></span></dt><dt><span class="sect2"><a href="libpq-exec.html#LIBPQ-EXEC-SELECT-INFO">33.3.2. Retrieving Query Result Information</a></span></dt><dt><span class="sect2"><a href="libpq-exec.html#LIBPQ-EXEC-NONSELECT">33.3.3. Retrieving Other Result Information</a></span></dt><dt><span class="sect2"><a href="libpq-exec.html#LIBPQ-EXEC-ESCAPE-STRING">33.3.4. Escaping Strings for Inclusion in SQL Commands</a></span></dt></dl></div><p>
  3. Once a connection to a database server has been successfully
  4. established, the functions described here are used to perform
  5. SQL queries and commands.
  6. </p><div class="sect2" id="LIBPQ-EXEC-MAIN"><div class="titlepage"><div><div><h3 class="title">33.3.1. Main Functions</h3></div></div></div><p>
  7. </p><div class="variablelist"><dl class="variablelist"><dt id="LIBPQ-PQEXEC"><span class="term">
  8. <code class="function">PQexec</code>
  9. <a id="id-1.7.3.10.3.2.1.1.1.2" class="indexterm"></a>
  10. </span></dt><dd><p>
  11. Submits a command to the server and waits for the result.
  12. </p><pre class="synopsis">
  13. PGresult *PQexec(PGconn *conn, const char *command);
  14. </pre><p>
  15. </p><p>
  16. Returns a <code class="structname">PGresult</code> pointer or possibly a null
  17. pointer. A non-null pointer will generally be returned except in
  18. out-of-memory conditions or serious errors such as inability to send
  19. the command to the server. The <code class="function">PQresultStatus</code> function
  20. should be called to check the return value for any errors (including
  21. the value of a null pointer, in which case it will return
  22. <code class="symbol">PGRES_FATAL_ERROR</code>). Use
  23. <code class="function">PQerrorMessage</code> to get more information about such
  24. errors.
  25. </p></dd></dl></div><p>
  26. The command string can include multiple SQL commands
  27. (separated by semicolons). Multiple queries sent in a single
  28. <code class="function">PQexec</code> call are processed in a single transaction, unless
  29. there are explicit <code class="command">BEGIN</code>/<code class="command">COMMIT</code>
  30. commands included in the query string to divide it into multiple
  31. transactions. (See <a class="xref" href="protocol-flow.html#PROTOCOL-FLOW-MULTI-STATEMENT" title="52.2.2.1. Multiple Statements in a Simple Query">Section 52.2.2.1</a>
  32. for more details about how the server handles multi-query strings.)
  33. Note however that the returned
  34. <code class="structname">PGresult</code> structure describes only the result
  35. of the last command executed from the string. Should one of the
  36. commands fail, processing of the string stops with it and the returned
  37. <code class="structname">PGresult</code> describes the error condition.
  38. </p><p>
  39. </p><div class="variablelist"><dl class="variablelist"><dt id="LIBPQ-PQEXECPARAMS"><span class="term">
  40. <code class="function">PQexecParams</code>
  41. <a id="id-1.7.3.10.3.3.1.1.1.2" class="indexterm"></a>
  42. </span></dt><dd><p>
  43. Submits a command to the server and waits for the result,
  44. with the ability to pass parameters separately from the SQL
  45. command text.
  46. </p><pre class="synopsis">
  47. PGresult *PQexecParams(PGconn *conn,
  48. const char *command,
  49. int nParams,
  50. const Oid *paramTypes,
  51. const char * const *paramValues,
  52. const int *paramLengths,
  53. const int *paramFormats,
  54. int resultFormat);
  55. </pre><p>
  56. </p><p>
  57. <code class="function">PQexecParams</code> is like <code class="function">PQexec</code>, but offers additional
  58. functionality: parameter values can be specified separately from the command
  59. string proper, and query results can be requested in either text or binary
  60. format. <code class="function">PQexecParams</code> is supported only in protocol 3.0 and later
  61. connections; it will fail when using protocol 2.0.
  62. </p><p>
  63. The function arguments are:
  64. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="parameter"><code>conn</code></em></span></dt><dd><p>
  65. The connection object to send the command through.
  66. </p></dd><dt><span class="term"><em class="parameter"><code>command</code></em></span></dt><dd><p>
  67. The SQL command string to be executed. If parameters are used,
  68. they are referred to in the command string as <code class="literal">$1</code>,
  69. <code class="literal">$2</code>, etc.
  70. </p></dd><dt><span class="term"><em class="parameter"><code>nParams</code></em></span></dt><dd><p>
  71. The number of parameters supplied; it is the length of the arrays
  72. <em class="parameter"><code>paramTypes[]</code></em>, <em class="parameter"><code>paramValues[]</code></em>,
  73. <em class="parameter"><code>paramLengths[]</code></em>, and <em class="parameter"><code>paramFormats[]</code></em>. (The
  74. array pointers can be <code class="symbol">NULL</code> when <em class="parameter"><code>nParams</code></em>
  75. is zero.)
  76. </p></dd><dt><span class="term"><em class="parameter"><code>paramTypes[]</code></em></span></dt><dd><p>
  77. Specifies, by OID, the data types to be assigned to the
  78. parameter symbols. If <em class="parameter"><code>paramTypes</code></em> is
  79. <code class="symbol">NULL</code>, or any particular element in the array
  80. is zero, the server infers a data type for the parameter symbol
  81. in the same way it would do for an untyped literal string.
  82. </p></dd><dt><span class="term"><em class="parameter"><code>paramValues[]</code></em></span></dt><dd><p>
  83. Specifies the actual values of the parameters. A null pointer
  84. in this array means the corresponding parameter is null;
  85. otherwise the pointer points to a zero-terminated text string
  86. (for text format) or binary data in the format expected by the
  87. server (for binary format).
  88. </p></dd><dt><span class="term"><em class="parameter"><code>paramLengths[]</code></em></span></dt><dd><p>
  89. Specifies the actual data lengths of binary-format parameters.
  90. It is ignored for null parameters and text-format parameters.
  91. The array pointer can be null when there are no binary parameters.
  92. </p></dd><dt><span class="term"><em class="parameter"><code>paramFormats[]</code></em></span></dt><dd><p>
  93. Specifies whether parameters are text (put a zero in the
  94. array entry for the corresponding parameter) or binary (put
  95. a one in the array entry for the corresponding parameter).
  96. If the array pointer is null then all parameters are presumed
  97. to be text strings.
  98. </p><p>
  99. Values passed in binary format require knowledge of
  100. the internal representation expected by the backend.
  101. For example, integers must be passed in network byte
  102. order. Passing <code class="type">numeric</code> values requires
  103. knowledge of the server storage format, as implemented
  104. in
  105. <code class="filename">src/backend/utils/adt/numeric.c::numeric_send()</code> and
  106. <code class="filename">src/backend/utils/adt/numeric.c::numeric_recv()</code>.
  107. </p></dd><dt><span class="term"><em class="parameter"><code>resultFormat</code></em></span></dt><dd><p>
  108. Specify zero to obtain results in text format, or one to obtain
  109. results in binary format. (There is not currently a provision
  110. to obtain different result columns in different formats,
  111. although that is possible in the underlying protocol.)
  112. </p></dd></dl></div><p>
  113. </p></dd></dl></div><p>
  114. </p><p>
  115. The primary advantage of <code class="function">PQexecParams</code> over
  116. <code class="function">PQexec</code> is that parameter values can be separated from the
  117. command string, thus avoiding the need for tedious and error-prone
  118. quoting and escaping.
  119. </p><p>
  120. Unlike <code class="function">PQexec</code>, <code class="function">PQexecParams</code> allows at most
  121. one SQL command in the given string. (There can be semicolons in it,
  122. but not more than one nonempty command.) This is a limitation of the
  123. underlying protocol, but has some usefulness as an extra defense against
  124. SQL-injection attacks.
  125. </p><div class="tip"><h3 class="title">Tip</h3><p>
  126. Specifying parameter types via OIDs is tedious, particularly if you prefer
  127. not to hard-wire particular OID values into your program. However, you can
  128. avoid doing so even in cases where the server by itself cannot determine the
  129. type of the parameter, or chooses a different type than you want. In the
  130. SQL command text, attach an explicit cast to the parameter symbol to show what
  131. data type you will send. For example:
  132. </p><pre class="programlisting">
  133. SELECT * FROM mytable WHERE x = $1::bigint;
  134. </pre><p>
  135. This forces parameter <code class="literal">$1</code> to be treated as <code class="type">bigint</code>, whereas
  136. by default it would be assigned the same type as <code class="literal">x</code>. Forcing the
  137. parameter type decision, either this way or by specifying a numeric type OID,
  138. is strongly recommended when sending parameter values in binary format, because
  139. binary format has less redundancy than text format and so there is less chance
  140. that the server will detect a type mismatch mistake for you.
  141. </p></div><p>
  142. </p><div class="variablelist"><dl class="variablelist"><dt id="LIBPQ-PQPREPARE"><span class="term"><code class="function">PQprepare</code>
  143. <a id="id-1.7.3.10.3.7.1.1.1.2" class="indexterm"></a>
  144. </span></dt><dd><p>
  145. Submits a request to create a prepared statement with the
  146. given parameters, and waits for completion.
  147. </p><pre class="synopsis">
  148. PGresult *PQprepare(PGconn *conn,
  149. const char *stmtName,
  150. const char *query,
  151. int nParams,
  152. const Oid *paramTypes);
  153. </pre><p>
  154. </p><p>
  155. <code class="function">PQprepare</code> creates a prepared statement for later
  156. execution with <code class="function">PQexecPrepared</code>. This feature allows
  157. commands to be executed repeatedly without being parsed and
  158. planned each time; see <a class="xref" href="sql-prepare.html" title="PREPARE"><span class="refentrytitle">PREPARE</span></a> for details.
  159. <code class="function">PQprepare</code> is supported only in protocol 3.0 and later
  160. connections; it will fail when using protocol 2.0.
  161. </p><p>
  162. The function creates a prepared statement named
  163. <em class="parameter"><code>stmtName</code></em> from the <em class="parameter"><code>query</code></em> string, which
  164. must contain a single SQL command. <em class="parameter"><code>stmtName</code></em> can be
  165. <code class="literal">""</code> to create an unnamed statement, in which case any
  166. pre-existing unnamed statement is automatically replaced; otherwise
  167. it is an error if the statement name is already defined in the
  168. current session. If any parameters are used, they are referred
  169. to in the query as <code class="literal">$1</code>, <code class="literal">$2</code>, etc.
  170. <em class="parameter"><code>nParams</code></em> is the number of parameters for which types
  171. are pre-specified in the array <em class="parameter"><code>paramTypes[]</code></em>. (The
  172. array pointer can be <code class="symbol">NULL</code> when
  173. <em class="parameter"><code>nParams</code></em> is zero.) <em class="parameter"><code>paramTypes[]</code></em>
  174. specifies, by OID, the data types to be assigned to the parameter
  175. symbols. If <em class="parameter"><code>paramTypes</code></em> is <code class="symbol">NULL</code>,
  176. or any particular element in the array is zero, the server assigns
  177. a data type to the parameter symbol in the same way it would do
  178. for an untyped literal string. Also, the query can use parameter
  179. symbols with numbers higher than <em class="parameter"><code>nParams</code></em>; data types
  180. will be inferred for these symbols as well. (See
  181. <code class="function">PQdescribePrepared</code> for a means to find out
  182. what data types were inferred.)
  183. </p><p>
  184. As with <code class="function">PQexec</code>, the result is normally a
  185. <code class="structname">PGresult</code> object whose contents indicate
  186. server-side success or failure. A null result indicates
  187. out-of-memory or inability to send the command at all. Use
  188. <code class="function">PQerrorMessage</code> to get more information about
  189. such errors.
  190. </p></dd></dl></div><p>
  191. Prepared statements for use with <code class="function">PQexecPrepared</code> can also
  192. be created by executing SQL <a class="xref" href="sql-prepare.html" title="PREPARE"><span class="refentrytitle">PREPARE</span></a>
  193. statements. Also, although there is no <span class="application">libpq</span>
  194. function for deleting a prepared statement, the SQL <a class="xref" href="sql-deallocate.html" title="DEALLOCATE"><span class="refentrytitle">DEALLOCATE</span></a> statement
  195. can be used for that purpose.
  196. </p><p>
  197. </p><div class="variablelist"><dl class="variablelist"><dt id="LIBPQ-PQEXECPREPARED"><span class="term">
  198. <code class="function">PQexecPrepared</code>
  199. <a id="id-1.7.3.10.3.8.1.1.1.2" class="indexterm"></a>
  200. </span></dt><dd><p>
  201. Sends a request to execute a prepared statement with given
  202. parameters, and waits for the result.
  203. </p><pre class="synopsis">
  204. PGresult *PQexecPrepared(PGconn *conn,
  205. const char *stmtName,
  206. int nParams,
  207. const char * const *paramValues,
  208. const int *paramLengths,
  209. const int *paramFormats,
  210. int resultFormat);
  211. </pre><p>
  212. </p><p>
  213. <code class="function">PQexecPrepared</code> is like <code class="function">PQexecParams</code>,
  214. but the command to be executed is specified by naming a
  215. previously-prepared statement, instead of giving a query string.
  216. This feature allows commands that will be used repeatedly to be
  217. parsed and planned just once, rather than each time they are
  218. executed. The statement must have been prepared previously in
  219. the current session. <code class="function">PQexecPrepared</code> is supported
  220. only in protocol 3.0 and later connections; it will fail when
  221. using protocol 2.0.
  222. </p><p>
  223. The parameters are identical to <code class="function">PQexecParams</code>, except that the
  224. name of a prepared statement is given instead of a query string, and the
  225. <em class="parameter"><code>paramTypes[]</code></em> parameter is not present (it is not needed since
  226. the prepared statement's parameter types were determined when it was created).
  227. </p></dd><dt id="LIBPQ-PQDESCRIBEPREPARED"><span class="term">
  228. <code class="function">PQdescribePrepared</code>
  229. <a id="id-1.7.3.10.3.8.1.2.1.2" class="indexterm"></a>
  230. </span></dt><dd><p>
  231. Submits a request to obtain information about the specified
  232. prepared statement, and waits for completion.
  233. </p><pre class="synopsis">
  234. PGresult *PQdescribePrepared(PGconn *conn, const char *stmtName);
  235. </pre><p>
  236. </p><p>
  237. <code class="function">PQdescribePrepared</code> allows an application to obtain
  238. information about a previously prepared statement.
  239. <code class="function">PQdescribePrepared</code> is supported only in protocol 3.0
  240. and later connections; it will fail when using protocol 2.0.
  241. </p><p>
  242. <em class="parameter"><code>stmtName</code></em> can be <code class="literal">""</code> or <code class="symbol">NULL</code> to reference
  243. the unnamed statement, otherwise it must be the name of an existing
  244. prepared statement. On success, a <code class="structname">PGresult</code> with
  245. status <code class="literal">PGRES_COMMAND_OK</code> is returned. The
  246. functions <code class="function">PQnparams</code> and
  247. <code class="function">PQparamtype</code> can be applied to this
  248. <code class="structname">PGresult</code> to obtain information about the parameters
  249. of the prepared statement, and the functions
  250. <code class="function">PQnfields</code>, <code class="function">PQfname</code>,
  251. <code class="function">PQftype</code>, etc provide information about the
  252. result columns (if any) of the statement.
  253. </p></dd><dt id="LIBPQ-PQDESCRIBEPORTAL"><span class="term">
  254. <code class="function">PQdescribePortal</code>
  255. <a id="id-1.7.3.10.3.8.1.3.1.2" class="indexterm"></a>
  256. </span></dt><dd><p>
  257. Submits a request to obtain information about the specified
  258. portal, and waits for completion.
  259. </p><pre class="synopsis">
  260. PGresult *PQdescribePortal(PGconn *conn, const char *portalName);
  261. </pre><p>
  262. </p><p>
  263. <code class="function">PQdescribePortal</code> allows an application to obtain
  264. information about a previously created portal.
  265. (<span class="application">libpq</span> does not provide any direct access to
  266. portals, but you can use this function to inspect the properties
  267. of a cursor created with a <code class="command">DECLARE CURSOR</code> SQL command.)
  268. <code class="function">PQdescribePortal</code> is supported only in protocol 3.0
  269. and later connections; it will fail when using protocol 2.0.
  270. </p><p>
  271. <em class="parameter"><code>portalName</code></em> can be <code class="literal">""</code> or <code class="symbol">NULL</code> to reference
  272. the unnamed portal, otherwise it must be the name of an existing
  273. portal. On success, a <code class="structname">PGresult</code> with status
  274. <code class="literal">PGRES_COMMAND_OK</code> is returned. The functions
  275. <code class="function">PQnfields</code>, <code class="function">PQfname</code>,
  276. <code class="function">PQftype</code>, etc can be applied to the
  277. <code class="structname">PGresult</code> to obtain information about the result
  278. columns (if any) of the portal.
  279. </p></dd></dl></div><p>
  280. </p><p>
  281. The <code class="structname">PGresult</code><a id="id-1.7.3.10.3.9.2" class="indexterm"></a>
  282. structure encapsulates the result returned by the server.
  283. <span class="application">libpq</span> application programmers should be
  284. careful to maintain the <code class="structname">PGresult</code> abstraction.
  285. Use the accessor functions below to get at the contents of
  286. <code class="structname">PGresult</code>. Avoid directly referencing the
  287. fields of the <code class="structname">PGresult</code> structure because they
  288. are subject to change in the future.
  289. </p><div class="variablelist"><dl class="variablelist"><dt id="LIBPQ-PQRESULTSTATUS"><span class="term">
  290. <code class="function">PQresultStatus</code>
  291. <a id="id-1.7.3.10.3.9.7.1.1.2" class="indexterm"></a>
  292. </span></dt><dd><p>
  293. Returns the result status of the command.
  294. </p><pre class="synopsis">
  295. ExecStatusType PQresultStatus(const PGresult *res);
  296. </pre><p>
  297. </p><p>
  298. <code class="function">PQresultStatus</code> can return one of the following values:
  299. </p><div class="variablelist"><dl class="variablelist"><dt id="LIBPQ-PGRES-EMPTY-QUERY"><span class="term"><code class="literal">PGRES_EMPTY_QUERY</code></span></dt><dd><p>
  300. The string sent to the server was empty.
  301. </p></dd><dt id="LIBPQ-PGRES-COMMAND-OK"><span class="term"><code class="literal">PGRES_COMMAND_OK</code></span></dt><dd><p>
  302. Successful completion of a command returning no data.
  303. </p></dd><dt id="LIBPQ-PGRES-TUPLES-OK"><span class="term"><code class="literal">PGRES_TUPLES_OK</code></span></dt><dd><p>
  304. Successful completion of a command returning data (such as
  305. a <code class="command">SELECT</code> or <code class="command">SHOW</code>).
  306. </p></dd><dt id="LIBPQ-PGRES-COPY-OUT"><span class="term"><code class="literal">PGRES_COPY_OUT</code></span></dt><dd><p>
  307. Copy Out (from server) data transfer started.
  308. </p></dd><dt id="LIBPQ-PGRES-COPY-IN"><span class="term"><code class="literal">PGRES_COPY_IN</code></span></dt><dd><p>
  309. Copy In (to server) data transfer started.
  310. </p></dd><dt id="LIBPQ-PGRES-BAD-RESPONSE"><span class="term"><code class="literal">PGRES_BAD_RESPONSE</code></span></dt><dd><p>
  311. The server's response was not understood.
  312. </p></dd><dt id="LIBPQ-PGRES-NONFATAL-ERROR"><span class="term"><code class="literal">PGRES_NONFATAL_ERROR</code></span></dt><dd><p>
  313. A nonfatal error (a notice or warning) occurred.
  314. </p></dd><dt id="LIBPQ-PGRES-FATAL-ERROR"><span class="term"><code class="literal">PGRES_FATAL_ERROR</code></span></dt><dd><p>
  315. A fatal error occurred.
  316. </p></dd><dt id="LIBPQ-PGRES-COPY-BOTH"><span class="term"><code class="literal">PGRES_COPY_BOTH</code></span></dt><dd><p>
  317. Copy In/Out (to and from server) data transfer started. This
  318. feature is currently used only for streaming replication,
  319. so this status should not occur in ordinary applications.
  320. </p></dd><dt id="LIBPQ-PGRES-SINGLE-TUPLE"><span class="term"><code class="literal">PGRES_SINGLE_TUPLE</code></span></dt><dd><p>
  321. The <code class="structname">PGresult</code> contains a single result tuple
  322. from the current command. This status occurs only when
  323. single-row mode has been selected for the query
  324. (see <a class="xref" href="libpq-single-row-mode.html" title="33.5. Retrieving Query Results Row-by-Row">Section 33.5</a>).
  325. </p></dd></dl></div><p>
  326. If the result status is <code class="literal">PGRES_TUPLES_OK</code> or
  327. <code class="literal">PGRES_SINGLE_TUPLE</code>, then
  328. the functions described below can be used to retrieve the rows
  329. returned by the query. Note that a <code class="command">SELECT</code>
  330. command that happens to retrieve zero rows still shows
  331. <code class="literal">PGRES_TUPLES_OK</code>.
  332. <code class="literal">PGRES_COMMAND_OK</code> is for commands that can never
  333. return rows (<code class="command">INSERT</code> or <code class="command">UPDATE</code>
  334. without a <code class="literal">RETURNING</code> clause,
  335. etc.). A response of <code class="literal">PGRES_EMPTY_QUERY</code> might
  336. indicate a bug in the client software.
  337. </p><p>
  338. A result of status <code class="symbol">PGRES_NONFATAL_ERROR</code> will
  339. never be returned directly by <code class="function">PQexec</code> or other
  340. query execution functions; results of this kind are instead passed
  341. to the notice processor (see <a class="xref" href="libpq-notice-processing.html" title="33.12. Notice Processing">Section 33.12</a>).
  342. </p></dd><dt id="LIBPQ-PQRESSTATUS"><span class="term">
  343. <code class="function">PQresStatus</code>
  344. <a id="id-1.7.3.10.3.9.7.2.1.2" class="indexterm"></a>
  345. </span></dt><dd><p>
  346. Converts the enumerated type returned by
  347. <code class="function">PQresultStatus</code> into a string constant describing the
  348. status code. The caller should not free the result.
  349. </p><pre class="synopsis">
  350. char *PQresStatus(ExecStatusType status);
  351. </pre><p>
  352. </p></dd><dt id="LIBPQ-PQRESULTERRORMESSAGE"><span class="term">
  353. <code class="function">PQresultErrorMessage</code>
  354. <a id="id-1.7.3.10.3.9.7.3.1.2" class="indexterm"></a>
  355. </span></dt><dd><p>
  356. Returns the error message associated with the command, or an empty string
  357. if there was no error.
  358. </p><pre class="synopsis">
  359. char *PQresultErrorMessage(const PGresult *res);
  360. </pre><p>
  361. If there was an error, the returned string will include a trailing
  362. newline. The caller should not free the result directly. It will
  363. be freed when the associated <code class="structname">PGresult</code> handle is
  364. passed to <code class="function">PQclear</code>.
  365. </p><p>
  366. Immediately following a <code class="function">PQexec</code> or
  367. <code class="function">PQgetResult</code> call,
  368. <code class="function">PQerrorMessage</code> (on the connection) will return
  369. the same string as <code class="function">PQresultErrorMessage</code> (on
  370. the result). However, a <code class="structname">PGresult</code> will
  371. retain its error message until destroyed, whereas the connection's
  372. error message will change when subsequent operations are done.
  373. Use <code class="function">PQresultErrorMessage</code> when you want to
  374. know the status associated with a particular
  375. <code class="structname">PGresult</code>; use
  376. <code class="function">PQerrorMessage</code> when you want to know the
  377. status from the latest operation on the connection.
  378. </p></dd><dt id="LIBPQ-PQRESULTVERBOSEERRORMESSAGE"><span class="term">
  379. <code class="function">PQresultVerboseErrorMessage</code>
  380. <a id="id-1.7.3.10.3.9.7.4.1.2" class="indexterm"></a>
  381. </span></dt><dd><p>
  382. Returns a reformatted version of the error message associated with
  383. a <code class="structname">PGresult</code> object.
  384. </p><pre class="synopsis">
  385. char *PQresultVerboseErrorMessage(const PGresult *res,
  386. PGVerbosity verbosity,
  387. PGContextVisibility show_context);
  388. </pre><p>
  389. In some situations a client might wish to obtain a more detailed
  390. version of a previously-reported error.
  391. <code class="function">PQresultVerboseErrorMessage</code> addresses this need
  392. by computing the message that would have been produced
  393. by <code class="function">PQresultErrorMessage</code> if the specified
  394. verbosity settings had been in effect for the connection when the
  395. given <code class="structname">PGresult</code> was generated. If
  396. the <code class="structname">PGresult</code> is not an error result,
  397. <span class="quote">“<span class="quote">PGresult is not an error result</span>”</span> is reported instead.
  398. The returned string includes a trailing newline.
  399. </p><p>
  400. Unlike most other functions for extracting data from
  401. a <code class="structname">PGresult</code>, the result of this function is a freshly
  402. allocated string. The caller must free it
  403. using <code class="function">PQfreemem()</code> when the string is no longer needed.
  404. </p><p>
  405. A NULL return is possible if there is insufficient memory.
  406. </p></dd><dt id="LIBPQ-PQRESULTERRORFIELD"><span class="term"><code class="function">PQresultErrorField</code><a id="id-1.7.3.10.3.9.7.5.1.2" class="indexterm"></a></span></dt><dd><p>
  407. Returns an individual field of an error report.
  408. </p><pre class="synopsis">
  409. char *PQresultErrorField(const PGresult *res, int fieldcode);
  410. </pre><p>
  411. <em class="parameter"><code>fieldcode</code></em> is an error field identifier; see the symbols
  412. listed below. <code class="symbol">NULL</code> is returned if the
  413. <code class="structname">PGresult</code> is not an error or warning result,
  414. or does not include the specified field. Field values will normally
  415. not include a trailing newline. The caller should not free the
  416. result directly. It will be freed when the
  417. associated <code class="structname">PGresult</code> handle is passed to
  418. <code class="function">PQclear</code>.
  419. </p><p>
  420. The following field codes are available:
  421. </p><div class="variablelist"><dl class="variablelist"><dt id="LIBPQ-PG-DIAG-SEVERITY"><span class="term"><code class="symbol">PG_DIAG_SEVERITY</code></span></dt><dd><p>
  422. The severity; the field contents are <code class="literal">ERROR</code>,
  423. <code class="literal">FATAL</code>, or <code class="literal">PANIC</code> (in an error message),
  424. or <code class="literal">WARNING</code>, <code class="literal">NOTICE</code>, <code class="literal">DEBUG</code>,
  425. <code class="literal">INFO</code>, or <code class="literal">LOG</code> (in a notice message), or
  426. a localized translation of one of these. Always present.
  427. </p></dd><dt id="LIBPQ-PG-DIAG-SEVERITY-NONLOCALIZED"><span class="term"><code class="symbol">PG_DIAG_SEVERITY_NONLOCALIZED</code></span></dt><dd><p>
  428. The severity; the field contents are <code class="literal">ERROR</code>,
  429. <code class="literal">FATAL</code>, or <code class="literal">PANIC</code> (in an error message),
  430. or <code class="literal">WARNING</code>, <code class="literal">NOTICE</code>, <code class="literal">DEBUG</code>,
  431. <code class="literal">INFO</code>, or <code class="literal">LOG</code> (in a notice message).
  432. This is identical to the <code class="symbol">PG_DIAG_SEVERITY</code> field except
  433. that the contents are never localized. This is present only in
  434. reports generated by <span class="productname">PostgreSQL</span> versions 9.6
  435. and later.
  436. </p></dd><dt id="LIBPQ-PG-DIAG-SQLSTATE"><span class="term">
  437. <code class="symbol">PG_DIAG_SQLSTATE</code>
  438. <a id="id-1.7.3.10.3.9.7.5.2.2.1.3.1.2" class="indexterm"></a>
  439. </span></dt><dd><p>
  440. The SQLSTATE code for the error. The SQLSTATE code identifies
  441. the type of error that has occurred; it can be used by
  442. front-end applications to perform specific operations (such
  443. as error handling) in response to a particular database error.
  444. For a list of the possible SQLSTATE codes, see <a class="xref" href="errcodes-appendix.html" title="Appendix A. PostgreSQL Error Codes">Appendix A</a>. This field is not localizable,
  445. and is always present.
  446. </p></dd><dt id="LIBPQ-PG-DIAG-MESSAGE-PRIMARY"><span class="term"><code class="symbol">PG_DIAG_MESSAGE_PRIMARY</code></span></dt><dd><p>
  447. The primary human-readable error message (typically one line).
  448. Always present.
  449. </p></dd><dt id="LIBPQ-PG-DIAG-MESSAGE-DETAIL"><span class="term"><code class="symbol">PG_DIAG_MESSAGE_DETAIL</code></span></dt><dd><p>
  450. Detail: an optional secondary error message carrying more
  451. detail about the problem. Might run to multiple lines.
  452. </p></dd><dt id="LIBPQ-PG-DIAG-MESSAGE-HINT"><span class="term"><code class="symbol">PG_DIAG_MESSAGE_HINT</code></span></dt><dd><p>
  453. Hint: an optional suggestion what to do about the problem.
  454. This is intended to differ from detail in that it offers advice
  455. (potentially inappropriate) rather than hard facts. Might
  456. run to multiple lines.
  457. </p></dd><dt id="LIBPQ-PG-DIAG-STATEMENT-POSITION"><span class="term"><code class="symbol">PG_DIAG_STATEMENT_POSITION</code></span></dt><dd><p>
  458. A string containing a decimal integer indicating an error cursor
  459. position as an index into the original statement string. The
  460. first character has index 1, and positions are measured in
  461. characters not bytes.
  462. </p></dd><dt id="LIBPQ-PG-DIAG-INTERNAL-POSITION"><span class="term"><code class="symbol">PG_DIAG_INTERNAL_POSITION</code></span></dt><dd><p>
  463. This is defined the same as the
  464. <code class="symbol">PG_DIAG_STATEMENT_POSITION</code> field, but it is used
  465. when the cursor position refers to an internally generated
  466. command rather than the one submitted by the client. The
  467. <code class="symbol">PG_DIAG_INTERNAL_QUERY</code> field will always appear when
  468. this field appears.
  469. </p></dd><dt id="LIBPQ-PG-DIAG-INTERNAL-QUERY"><span class="term"><code class="symbol">PG_DIAG_INTERNAL_QUERY</code></span></dt><dd><p>
  470. The text of a failed internally-generated command. This could
  471. be, for example, a SQL query issued by a PL/pgSQL function.
  472. </p></dd><dt id="LIBPQ-PG-DIAG-CONTEXT"><span class="term"><code class="symbol">PG_DIAG_CONTEXT</code></span></dt><dd><p>
  473. An indication of the context in which the error occurred.
  474. Presently this includes a call stack traceback of active
  475. procedural language functions and internally-generated queries.
  476. The trace is one entry per line, most recent first.
  477. </p></dd><dt id="LIBPQ-PG-DIAG-SCHEMA-NAME"><span class="term"><code class="symbol">PG_DIAG_SCHEMA_NAME</code></span></dt><dd><p>
  478. If the error was associated with a specific database object,
  479. the name of the schema containing that object, if any.
  480. </p></dd><dt id="LIBPQ-PG-DIAG-TABLE-NAME"><span class="term"><code class="symbol">PG_DIAG_TABLE_NAME</code></span></dt><dd><p>
  481. If the error was associated with a specific table, the name of the
  482. table. (Refer to the schema name field for the name of the
  483. table's schema.)
  484. </p></dd><dt id="LIBPQ-PG-DIAG-COLUMN-NAME"><span class="term"><code class="symbol">PG_DIAG_COLUMN_NAME</code></span></dt><dd><p>
  485. If the error was associated with a specific table column, the name
  486. of the column. (Refer to the schema and table name fields to
  487. identify the table.)
  488. </p></dd><dt id="LIBPQ-PG-DIAG-DATATYPE-NAME"><span class="term"><code class="symbol">PG_DIAG_DATATYPE_NAME</code></span></dt><dd><p>
  489. If the error was associated with a specific data type, the name of
  490. the data type. (Refer to the schema name field for the name of
  491. the data type's schema.)
  492. </p></dd><dt id="LIBPQ-PG-DIAG-CONSTRAINT-NAME"><span class="term"><code class="symbol">PG_DIAG_CONSTRAINT_NAME</code></span></dt><dd><p>
  493. If the error was associated with a specific constraint, the name
  494. of the constraint. Refer to fields listed above for the
  495. associated table or domain. (For this purpose, indexes are
  496. treated as constraints, even if they weren't created with
  497. constraint syntax.)
  498. </p></dd><dt id="LIBPQ-PG-DIAG-SOURCE-FILE"><span class="term"><code class="symbol">PG_DIAG_SOURCE_FILE</code></span></dt><dd><p>
  499. The file name of the source-code location where the error was
  500. reported.
  501. </p></dd><dt id="LIBPQ-PG-DIAG-SOURCE-LINE"><span class="term"><code class="symbol">PG_DIAG_SOURCE_LINE</code></span></dt><dd><p>
  502. The line number of the source-code location where the error
  503. was reported.
  504. </p></dd><dt id="LIBPQ-PG-DIAG-SOURCE-FUNCTION"><span class="term"><code class="symbol">PG_DIAG_SOURCE_FUNCTION</code></span></dt><dd><p>
  505. The name of the source-code function reporting the error.
  506. </p></dd></dl></div><p>
  507. </p><div class="note"><h3 class="title">Note</h3><p>
  508. The fields for schema name, table name, column name, data type name,
  509. and constraint name are supplied only for a limited number of error
  510. types; see <a class="xref" href="errcodes-appendix.html" title="Appendix A. PostgreSQL Error Codes">Appendix A</a>. Do not assume that
  511. the presence of any of these fields guarantees the presence of
  512. another field. Core error sources observe the interrelationships
  513. noted above, but user-defined functions may use these fields in other
  514. ways. In the same vein, do not assume that these fields denote
  515. contemporary objects in the current database.
  516. </p></div><p>
  517. The client is responsible for formatting displayed information to meet
  518. its needs; in particular it should break long lines as needed.
  519. Newline characters appearing in the error message fields should be
  520. treated as paragraph breaks, not line breaks.
  521. </p><p>
  522. Errors generated internally by <span class="application">libpq</span> will
  523. have severity and primary message, but typically no other fields.
  524. Errors returned by a pre-3.0-protocol server will include severity and
  525. primary message, and sometimes a detail message, but no other fields.
  526. </p><p>
  527. Note that error fields are only available from
  528. <code class="structname">PGresult</code> objects, not
  529. <code class="structname">PGconn</code> objects; there is no
  530. <code class="function">PQerrorField</code> function.
  531. </p></dd><dt id="LIBPQ-PQCLEAR"><span class="term"><code class="function">PQclear</code><a id="id-1.7.3.10.3.9.7.6.1.2" class="indexterm"></a></span></dt><dd><p>
  532. Frees the storage associated with a
  533. <code class="structname">PGresult</code>. Every command result should be
  534. freed via <code class="function">PQclear</code> when it is no longer
  535. needed.
  536. </p><pre class="synopsis">
  537. void PQclear(PGresult *res);
  538. </pre><p>
  539. </p><p>
  540. You can keep a <code class="structname">PGresult</code> object around for
  541. as long as you need it; it does not go away when you issue a new
  542. command, nor even if you close the connection. To get rid of it,
  543. you must call <code class="function">PQclear</code>. Failure to do this
  544. will result in memory leaks in your application.
  545. </p></dd></dl></div><p>
  546. </p></div><div class="sect2" id="LIBPQ-EXEC-SELECT-INFO"><div class="titlepage"><div><div><h3 class="title">33.3.2. Retrieving Query Result Information</h3></div></div></div><p>
  547. These functions are used to extract information from a
  548. <code class="structname">PGresult</code> object that represents a successful
  549. query result (that is, one that has status
  550. <code class="literal">PGRES_TUPLES_OK</code> or <code class="literal">PGRES_SINGLE_TUPLE</code>).
  551. They can also be used to extract
  552. information from a successful Describe operation: a Describe's result
  553. has all the same column information that actual execution of the query
  554. would provide, but it has zero rows. For objects with other status values,
  555. these functions will act as though the result has zero rows and zero columns.
  556. </p><div class="variablelist"><dl class="variablelist"><dt id="LIBPQ-PQNTUPLES"><span class="term">
  557. <code class="function">PQntuples</code>
  558. <a id="id-1.7.3.10.4.3.1.1.2" class="indexterm"></a>
  559. </span></dt><dd><p>
  560. Returns the number of rows (tuples) in the query result.
  561. (Note that <code class="structname">PGresult</code> objects are limited to no more
  562. than <code class="literal">INT_MAX</code> rows, so an <code class="type">int</code> result is
  563. sufficient.)
  564. </p><pre class="synopsis">
  565. int PQntuples(const PGresult *res);
  566. </pre><p>
  567. </p></dd><dt id="LIBPQ-PQNFIELDS"><span class="term">
  568. <code class="function">PQnfields</code>
  569. <a id="id-1.7.3.10.4.3.2.1.2" class="indexterm"></a>
  570. </span></dt><dd><p>
  571. Returns the number of columns (fields) in each row of the query
  572. result.
  573. </p><pre class="synopsis">
  574. int PQnfields(const PGresult *res);
  575. </pre><p>
  576. </p></dd><dt id="LIBPQ-PQFNAME"><span class="term">
  577. <code class="function">PQfname</code>
  578. <a id="id-1.7.3.10.4.3.3.1.2" class="indexterm"></a>
  579. </span></dt><dd><p>
  580. Returns the column name associated with the given column number.
  581. Column numbers start at 0. The caller should not free the result
  582. directly. It will be freed when the associated
  583. <code class="structname">PGresult</code> handle is passed to
  584. <code class="function">PQclear</code>.
  585. </p><pre class="synopsis">
  586. char *PQfname(const PGresult *res,
  587. int column_number);
  588. </pre><p>
  589. </p><p>
  590. <code class="symbol">NULL</code> is returned if the column number is out of range.
  591. </p></dd><dt id="LIBPQ-PQFNUMBER"><span class="term">
  592. <code class="function">PQfnumber</code>
  593. <a id="id-1.7.3.10.4.3.4.1.2" class="indexterm"></a>
  594. </span></dt><dd><p>
  595. Returns the column number associated with the given column name.
  596. </p><pre class="synopsis">
  597. int PQfnumber(const PGresult *res,
  598. const char *column_name);
  599. </pre><p>
  600. </p><p>
  601. -1 is returned if the given name does not match any column.
  602. </p><p>
  603. The given name is treated like an identifier in an SQL command,
  604. that is, it is downcased unless double-quoted. For example, given
  605. a query result generated from the SQL command:
  606. </p><pre class="programlisting">
  607. SELECT 1 AS FOO, 2 AS "BAR";
  608. </pre><p>
  609. we would have the results:
  610. </p><pre class="programlisting">
  611. PQfname(res, 0) <em class="lineannotation"><span class="lineannotation">foo</span></em>
  612. PQfname(res, 1) <em class="lineannotation"><span class="lineannotation">BAR</span></em>
  613. PQfnumber(res, "FOO") <em class="lineannotation"><span class="lineannotation">0</span></em>
  614. PQfnumber(res, "foo") <em class="lineannotation"><span class="lineannotation">0</span></em>
  615. PQfnumber(res, "BAR") <em class="lineannotation"><span class="lineannotation">-1</span></em>
  616. PQfnumber(res, "\"BAR\"") <em class="lineannotation"><span class="lineannotation">1</span></em>
  617. </pre><p>
  618. </p></dd><dt id="LIBPQ-PQFTABLE"><span class="term">
  619. <code class="function">PQftable</code>
  620. <a id="id-1.7.3.10.4.3.5.1.2" class="indexterm"></a>
  621. </span></dt><dd><p>
  622. Returns the OID of the table from which the given column was
  623. fetched. Column numbers start at 0.
  624. </p><pre class="synopsis">
  625. Oid PQftable(const PGresult *res,
  626. int column_number);
  627. </pre><p>
  628. </p><p>
  629. <code class="literal">InvalidOid</code> is returned if the column number is out of range,
  630. or if the specified column is not a simple reference to a table column,
  631. or when using pre-3.0 protocol.
  632. You can query the system table <code class="literal">pg_class</code> to determine
  633. exactly which table is referenced.
  634. </p><p>
  635. The type <code class="type">Oid</code> and the constant
  636. <code class="literal">InvalidOid</code> will be defined when you include
  637. the <span class="application">libpq</span> header file. They will both
  638. be some integer type.
  639. </p></dd><dt id="LIBPQ-PQFTABLECOL"><span class="term">
  640. <code class="function">PQftablecol</code>
  641. <a id="id-1.7.3.10.4.3.6.1.2" class="indexterm"></a>
  642. </span></dt><dd><p>
  643. Returns the column number (within its table) of the column making
  644. up the specified query result column. Query-result column numbers
  645. start at 0, but table columns have nonzero numbers.
  646. </p><pre class="synopsis">
  647. int PQftablecol(const PGresult *res,
  648. int column_number);
  649. </pre><p>
  650. </p><p>
  651. Zero is returned if the column number is out of range, or if the
  652. specified column is not a simple reference to a table column, or
  653. when using pre-3.0 protocol.
  654. </p></dd><dt id="LIBPQ-PQFFORMAT"><span class="term">
  655. <code class="function">PQfformat</code>
  656. <a id="id-1.7.3.10.4.3.7.1.2" class="indexterm"></a>
  657. </span></dt><dd><p>
  658. Returns the format code indicating the format of the given
  659. column. Column numbers start at 0.
  660. </p><pre class="synopsis">
  661. int PQfformat(const PGresult *res,
  662. int column_number);
  663. </pre><p>
  664. </p><p>
  665. Format code zero indicates textual data representation, while format
  666. code one indicates binary representation. (Other codes are reserved
  667. for future definition.)
  668. </p></dd><dt id="LIBPQ-PQFTYPE"><span class="term">
  669. <code class="function">PQftype</code>
  670. <a id="id-1.7.3.10.4.3.8.1.2" class="indexterm"></a>
  671. </span></dt><dd><p>
  672. Returns the data type associated with the given column number.
  673. The integer returned is the internal OID number of the type.
  674. Column numbers start at 0.
  675. </p><pre class="synopsis">
  676. Oid PQftype(const PGresult *res,
  677. int column_number);
  678. </pre><p>
  679. </p><p>
  680. You can query the system table <code class="literal">pg_type</code> to
  681. obtain the names and properties of the various data types. The
  682. <acronym class="acronym">OID</acronym>s of the built-in data types are defined
  683. in the file <code class="filename">src/include/catalog/pg_type_d.h</code>
  684. in the source tree.
  685. </p></dd><dt id="LIBPQ-PQFMOD"><span class="term">
  686. <code class="function">PQfmod</code>
  687. <a id="id-1.7.3.10.4.3.9.1.2" class="indexterm"></a>
  688. </span></dt><dd><p>
  689. Returns the type modifier of the column associated with the
  690. given column number. Column numbers start at 0.
  691. </p><pre class="synopsis">
  692. int PQfmod(const PGresult *res,
  693. int column_number);
  694. </pre><p>
  695. </p><p>
  696. The interpretation of modifier values is type-specific; they
  697. typically indicate precision or size limits. The value -1 is
  698. used to indicate <span class="quote">“<span class="quote">no information available</span>”</span>. Most data
  699. types do not use modifiers, in which case the value is always
  700. -1.
  701. </p></dd><dt id="LIBPQ-PQFSIZE"><span class="term">
  702. <code class="function">PQfsize</code>
  703. <a id="id-1.7.3.10.4.3.10.1.2" class="indexterm"></a>
  704. </span></dt><dd><p>
  705. Returns the size in bytes of the column associated with the
  706. given column number. Column numbers start at 0.
  707. </p><pre class="synopsis">
  708. int PQfsize(const PGresult *res,
  709. int column_number);
  710. </pre><p>
  711. </p><p>
  712. <code class="function">PQfsize</code> returns the space allocated for this column
  713. in a database row, in other words the size of the server's
  714. internal representation of the data type. (Accordingly, it is
  715. not really very useful to clients.) A negative value indicates
  716. the data type is variable-length.
  717. </p></dd><dt id="LIBPQ-PQBINARYTUPLES"><span class="term">
  718. <code class="function">PQbinaryTuples</code>
  719. <a id="id-1.7.3.10.4.3.11.1.2" class="indexterm"></a>
  720. </span></dt><dd><p>
  721. Returns 1 if the <code class="structname">PGresult</code> contains binary data
  722. and 0 if it contains text data.
  723. </p><pre class="synopsis">
  724. int PQbinaryTuples(const PGresult *res);
  725. </pre><p>
  726. </p><p>
  727. This function is deprecated (except for its use in connection with
  728. <code class="command">COPY</code>), because it is possible for a single
  729. <code class="structname">PGresult</code> to contain text data in some columns and
  730. binary data in others. <code class="function">PQfformat</code> is preferred.
  731. <code class="function">PQbinaryTuples</code> returns 1 only if all columns of the
  732. result are binary (format 1).
  733. </p></dd><dt id="LIBPQ-PQGETVALUE"><span class="term">
  734. <code class="function">PQgetvalue</code>
  735. <a id="id-1.7.3.10.4.3.12.1.2" class="indexterm"></a>
  736. </span></dt><dd><p>
  737. Returns a single field value of one row of a
  738. <code class="structname">PGresult</code>. Row and column numbers start
  739. at 0. The caller should not free the result directly. It will
  740. be freed when the associated <code class="structname">PGresult</code> handle is
  741. passed to <code class="function">PQclear</code>.
  742. </p><pre class="synopsis">
  743. char *PQgetvalue(const PGresult *res,
  744. int row_number,
  745. int column_number);
  746. </pre><p>
  747. </p><p>
  748. For data in text format, the value returned by
  749. <code class="function">PQgetvalue</code> is a null-terminated character
  750. string representation of the field value. For data in binary
  751. format, the value is in the binary representation determined by
  752. the data type's <code class="function">typsend</code> and <code class="function">typreceive</code>
  753. functions. (The value is actually followed by a zero byte in
  754. this case too, but that is not ordinarily useful, since the
  755. value is likely to contain embedded nulls.)
  756. </p><p>
  757. An empty string is returned if the field value is null. See
  758. <code class="function">PQgetisnull</code> to distinguish null values from
  759. empty-string values.
  760. </p><p>
  761. The pointer returned by <code class="function">PQgetvalue</code> points
  762. to storage that is part of the <code class="structname">PGresult</code>
  763. structure. One should not modify the data it points to, and one
  764. must explicitly copy the data into other storage if it is to be
  765. used past the lifetime of the <code class="structname">PGresult</code>
  766. structure itself.
  767. </p></dd><dt id="LIBPQ-PQGETISNULL"><span class="term">
  768. <code class="function">PQgetisnull</code>
  769. <a id="id-1.7.3.10.4.3.13.1.2" class="indexterm"></a>
  770. <a id="id-1.7.3.10.4.3.13.1.3" class="indexterm"></a>
  771. </span></dt><dd><p>
  772. Tests a field for a null value. Row and column numbers start
  773. at 0.
  774. </p><pre class="synopsis">
  775. int PQgetisnull(const PGresult *res,
  776. int row_number,
  777. int column_number);
  778. </pre><p>
  779. </p><p>
  780. This function returns 1 if the field is null and 0 if it
  781. contains a non-null value. (Note that
  782. <code class="function">PQgetvalue</code> will return an empty string,
  783. not a null pointer, for a null field.)
  784. </p></dd><dt id="LIBPQ-PQGETLENGTH"><span class="term">
  785. <code class="function">PQgetlength</code>
  786. <a id="id-1.7.3.10.4.3.14.1.2" class="indexterm"></a></span></dt><dd><p>
  787. Returns the actual length of a field value in bytes. Row and
  788. column numbers start at 0.
  789. </p><pre class="synopsis">
  790. int PQgetlength(const PGresult *res,
  791. int row_number,
  792. int column_number);
  793. </pre><p>
  794. </p><p>
  795. This is the actual data length for the particular data value,
  796. that is, the size of the object pointed to by
  797. <code class="function">PQgetvalue</code>. For text data format this is
  798. the same as <code class="function">strlen()</code>. For binary format this is
  799. essential information. Note that one should <span class="emphasis"><em>not</em></span>
  800. rely on <code class="function">PQfsize</code> to obtain the actual data
  801. length.
  802. </p></dd><dt id="LIBPQ-PQNPARAMS"><span class="term">
  803. <code class="function">PQnparams</code>
  804. <a id="id-1.7.3.10.4.3.15.1.2" class="indexterm"></a>
  805. </span></dt><dd><p>
  806. Returns the number of parameters of a prepared statement.
  807. </p><pre class="synopsis">
  808. int PQnparams(const PGresult *res);
  809. </pre><p>
  810. </p><p>
  811. This function is only useful when inspecting the result of
  812. <code class="function">PQdescribePrepared</code>. For other types of queries it
  813. will return zero.
  814. </p></dd><dt id="LIBPQ-PQPARAMTYPE"><span class="term">
  815. <code class="function">PQparamtype</code>
  816. <a id="id-1.7.3.10.4.3.16.1.2" class="indexterm"></a>
  817. </span></dt><dd><p>
  818. Returns the data type of the indicated statement parameter.
  819. Parameter numbers start at 0.
  820. </p><pre class="synopsis">
  821. Oid PQparamtype(const PGresult *res, int param_number);
  822. </pre><p>
  823. </p><p>
  824. This function is only useful when inspecting the result of
  825. <code class="function">PQdescribePrepared</code>. For other types of queries it
  826. will return zero.
  827. </p></dd><dt id="LIBPQ-PQPRINT"><span class="term">
  828. <code class="function">PQprint</code>
  829. <a id="id-1.7.3.10.4.3.17.1.2" class="indexterm"></a>
  830. </span></dt><dd><p>
  831. Prints out all the rows and, optionally, the column names to
  832. the specified output stream.
  833. </p><pre class="synopsis">
  834. void PQprint(FILE *fout, /* output stream */
  835. const PGresult *res,
  836. const PQprintOpt *po);
  837. typedef struct
  838. {
  839. pqbool header; /* print output field headings and row count */
  840. pqbool align; /* fill align the fields */
  841. pqbool standard; /* old brain dead format */
  842. pqbool html3; /* output HTML tables */
  843. pqbool expanded; /* expand tables */
  844. pqbool pager; /* use pager for output if needed */
  845. char *fieldSep; /* field separator */
  846. char *tableOpt; /* attributes for HTML table element */
  847. char *caption; /* HTML table caption */
  848. char **fieldName; /* null-terminated array of replacement field names */
  849. } PQprintOpt;
  850. </pre><p>
  851. </p><p>
  852. This function was formerly used by <span class="application">psql</span>
  853. to print query results, but this is no longer the case. Note
  854. that it assumes all the data is in text format.
  855. </p></dd></dl></div></div><div class="sect2" id="LIBPQ-EXEC-NONSELECT"><div class="titlepage"><div><div><h3 class="title">33.3.3. Retrieving Other Result Information</h3></div></div></div><p>
  856. These functions are used to extract other information from
  857. <code class="structname">PGresult</code> objects.
  858. </p><div class="variablelist"><dl class="variablelist"><dt id="LIBPQ-PQCMDSTATUS"><span class="term">
  859. <code class="function">PQcmdStatus</code>
  860. <a id="id-1.7.3.10.5.3.1.1.2" class="indexterm"></a>
  861. </span></dt><dd><p>
  862. Returns the command status tag from the SQL command that generated
  863. the <code class="structname">PGresult</code>.
  864. </p><pre class="synopsis">
  865. char *PQcmdStatus(PGresult *res);
  866. </pre><p>
  867. </p><p>
  868. Commonly this is just the name of the command, but it might include
  869. additional data such as the number of rows processed. The caller
  870. should not free the result directly. It will be freed when the
  871. associated <code class="structname">PGresult</code> handle is passed to
  872. <code class="function">PQclear</code>.
  873. </p></dd><dt id="LIBPQ-PQCMDTUPLES"><span class="term">
  874. <code class="function">PQcmdTuples</code>
  875. <a id="id-1.7.3.10.5.3.2.1.2" class="indexterm"></a>
  876. </span></dt><dd><p>
  877. Returns the number of rows affected by the SQL command.
  878. </p><pre class="synopsis">
  879. char *PQcmdTuples(PGresult *res);
  880. </pre><p>
  881. </p><p>
  882. This function returns a string containing the number of rows
  883. affected by the <acronym class="acronym">SQL</acronym> statement that generated the
  884. <code class="structname">PGresult</code>. This function can only be used following
  885. the execution of a <code class="command">SELECT</code>, <code class="command">CREATE TABLE AS</code>,
  886. <code class="command">INSERT</code>, <code class="command">UPDATE</code>, <code class="command">DELETE</code>,
  887. <code class="command">MOVE</code>, <code class="command">FETCH</code>, or <code class="command">COPY</code> statement,
  888. or an <code class="command">EXECUTE</code> of a prepared query that contains an
  889. <code class="command">INSERT</code>, <code class="command">UPDATE</code>, or <code class="command">DELETE</code> statement.
  890. If the command that generated the <code class="structname">PGresult</code> was anything
  891. else, <code class="function">PQcmdTuples</code> returns an empty string. The caller
  892. should not free the return value directly. It will be freed when
  893. the associated <code class="structname">PGresult</code> handle is passed to
  894. <code class="function">PQclear</code>.
  895. </p></dd><dt id="LIBPQ-PQOIDVALUE"><span class="term">
  896. <code class="function">PQoidValue</code>
  897. <a id="id-1.7.3.10.5.3.3.1.2" class="indexterm"></a>
  898. </span></dt><dd><p>
  899. Returns the OID<a id="id-1.7.3.10.5.3.3.2.1.1" class="indexterm"></a>
  900. of the inserted row, if the <acronym class="acronym">SQL</acronym> command was an
  901. <code class="command">INSERT</code> that inserted exactly one row into a table that
  902. has OIDs, or a <code class="command">EXECUTE</code> of a prepared query containing
  903. a suitable <code class="command">INSERT</code> statement. Otherwise, this function
  904. returns <code class="literal">InvalidOid</code>. This function will also
  905. return <code class="literal">InvalidOid</code> if the table affected by the
  906. <code class="command">INSERT</code> statement does not contain OIDs.
  907. </p><pre class="synopsis">
  908. Oid PQoidValue(const PGresult *res);
  909. </pre><p>
  910. </p></dd><dt id="LIBPQ-PQOIDSTATUS"><span class="term">
  911. <code class="function">PQoidStatus</code>
  912. <a id="id-1.7.3.10.5.3.4.1.2" class="indexterm"></a>
  913. </span></dt><dd><p>
  914. This function is deprecated in favor of
  915. <code class="function">PQoidValue</code> and is not thread-safe.
  916. It returns a string with the OID of the inserted row, while
  917. <code class="function">PQoidValue</code> returns the OID value.
  918. </p><pre class="synopsis">
  919. char *PQoidStatus(const PGresult *res);
  920. </pre><p>
  921. </p></dd></dl></div></div><div class="sect2" id="LIBPQ-EXEC-ESCAPE-STRING"><div class="titlepage"><div><div><h3 class="title">33.3.4. Escaping Strings for Inclusion in SQL Commands</h3></div></div></div><a id="id-1.7.3.10.6.2" class="indexterm"></a><div class="variablelist"><dl class="variablelist"><dt id="LIBPQ-PQESCAPELITERAL"><span class="term">
  922. <code class="function">PQescapeLiteral</code>
  923. <a id="id-1.7.3.10.6.3.1.1.2" class="indexterm"></a>
  924. </span></dt><dd><p>
  925. </p><pre class="synopsis">
  926. char *PQescapeLiteral(PGconn *conn, const char *str, size_t length);
  927. </pre><p>
  928. </p><p>
  929. <code class="function">PQescapeLiteral</code> escapes a string for
  930. use within an SQL command. This is useful when inserting data
  931. values as literal constants in SQL commands. Certain characters
  932. (such as quotes and backslashes) must be escaped to prevent them
  933. from being interpreted specially by the SQL parser.
  934. <code class="function">PQescapeLiteral</code> performs this operation.
  935. </p><p>
  936. <code class="function">PQescapeLiteral</code> returns an escaped version of the
  937. <em class="parameter"><code>str</code></em> parameter in memory allocated with
  938. <code class="function">malloc()</code>. This memory should be freed using
  939. <code class="function">PQfreemem()</code> when the result is no longer needed.
  940. A terminating zero byte is not required, and should not be
  941. counted in <em class="parameter"><code>length</code></em>. (If a terminating zero byte is found
  942. before <em class="parameter"><code>length</code></em> bytes are processed,
  943. <code class="function">PQescapeLiteral</code> stops at the zero; the behavior is
  944. thus rather like <code class="function">strncpy</code>.) The
  945. return string has all special characters replaced so that they can
  946. be properly processed by the <span class="productname">PostgreSQL</span>
  947. string literal parser. A terminating zero byte is also added. The
  948. single quotes that must surround <span class="productname">PostgreSQL</span>
  949. string literals are included in the result string.
  950. </p><p>
  951. On error, <code class="function">PQescapeLiteral</code> returns <code class="symbol">NULL</code> and a suitable
  952. message is stored in the <em class="parameter"><code>conn</code></em> object.
  953. </p><div class="tip"><h3 class="title">Tip</h3><p>
  954. It is especially important to do proper escaping when handling
  955. strings that were received from an untrustworthy source.
  956. Otherwise there is a security risk: you are vulnerable to
  957. <span class="quote">“<span class="quote">SQL injection</span>”</span> attacks wherein unwanted SQL commands are
  958. fed to your database.
  959. </p></div><p>
  960. Note that it is neither necessary nor correct to do escaping when a data
  961. value is passed as a separate parameter in <code class="function">PQexecParams</code> or
  962. its sibling routines.
  963. </p></dd><dt id="LIBPQ-PQESCAPEIDENTIFIER"><span class="term">
  964. <code class="function">PQescapeIdentifier</code>
  965. <a id="id-1.7.3.10.6.3.2.1.2" class="indexterm"></a>
  966. </span></dt><dd><p>
  967. </p><pre class="synopsis">
  968. char *PQescapeIdentifier(PGconn *conn, const char *str, size_t length);
  969. </pre><p>
  970. </p><p>
  971. <code class="function">PQescapeIdentifier</code> escapes a string for
  972. use as an SQL identifier, such as a table, column, or function name.
  973. This is useful when a user-supplied identifier might contain
  974. special characters that would otherwise not be interpreted as part
  975. of the identifier by the SQL parser, or when the identifier might
  976. contain upper case characters whose case should be preserved.
  977. </p><p>
  978. <code class="function">PQescapeIdentifier</code> returns a version of the
  979. <em class="parameter"><code>str</code></em> parameter escaped as an SQL identifier
  980. in memory allocated with <code class="function">malloc()</code>. This memory must be
  981. freed using <code class="function">PQfreemem()</code> when the result is no longer
  982. needed. A terminating zero byte is not required, and should not be
  983. counted in <em class="parameter"><code>length</code></em>. (If a terminating zero byte is found
  984. before <em class="parameter"><code>length</code></em> bytes are processed,
  985. <code class="function">PQescapeIdentifier</code> stops at the zero; the behavior is
  986. thus rather like <code class="function">strncpy</code>.) The
  987. return string has all special characters replaced so that it
  988. will be properly processed as an SQL identifier. A terminating zero byte
  989. is also added. The return string will also be surrounded by double
  990. quotes.
  991. </p><p>
  992. On error, <code class="function">PQescapeIdentifier</code> returns <code class="symbol">NULL</code> and a suitable
  993. message is stored in the <em class="parameter"><code>conn</code></em> object.
  994. </p><div class="tip"><h3 class="title">Tip</h3><p>
  995. As with string literals, to prevent SQL injection attacks,
  996. SQL identifiers must be escaped when they are received from an
  997. untrustworthy source.
  998. </p></div></dd><dt id="LIBPQ-PQESCAPESTRINGCONN"><span class="term">
  999. <code class="function">PQescapeStringConn</code>
  1000. <a id="id-1.7.3.10.6.3.3.1.2" class="indexterm"></a>
  1001. </span></dt><dd><p>
  1002. </p><pre class="synopsis">
  1003. size_t PQescapeStringConn(PGconn *conn,
  1004. char *to, const char *from, size_t length,
  1005. int *error);
  1006. </pre><p>
  1007. </p><p>
  1008. <code class="function">PQescapeStringConn</code> escapes string literals, much like
  1009. <code class="function">PQescapeLiteral</code>. Unlike <code class="function">PQescapeLiteral</code>,
  1010. the caller is responsible for providing an appropriately sized buffer.
  1011. Furthermore, <code class="function">PQescapeStringConn</code> does not generate the
  1012. single quotes that must surround <span class="productname">PostgreSQL</span> string
  1013. literals; they should be provided in the SQL command that the
  1014. result is inserted into. The parameter <em class="parameter"><code>from</code></em> points to
  1015. the first character of the string that is to be escaped, and the
  1016. <em class="parameter"><code>length</code></em> parameter gives the number of bytes in this
  1017. string. A terminating zero byte is not required, and should not be
  1018. counted in <em class="parameter"><code>length</code></em>. (If a terminating zero byte is found
  1019. before <em class="parameter"><code>length</code></em> bytes are processed,
  1020. <code class="function">PQescapeStringConn</code> stops at the zero; the behavior is
  1021. thus rather like <code class="function">strncpy</code>.) <em class="parameter"><code>to</code></em> shall point
  1022. to a buffer that is able to hold at least one more byte than twice
  1023. the value of <em class="parameter"><code>length</code></em>, otherwise the behavior is undefined.
  1024. Behavior is likewise undefined if the <em class="parameter"><code>to</code></em> and
  1025. <em class="parameter"><code>from</code></em> strings overlap.
  1026. </p><p>
  1027. If the <em class="parameter"><code>error</code></em> parameter is not <code class="symbol">NULL</code>, then
  1028. <code class="literal">*error</code> is set to zero on success, nonzero on error.
  1029. Presently the only possible error conditions involve invalid multibyte
  1030. encoding in the source string. The output string is still generated
  1031. on error, but it can be expected that the server will reject it as
  1032. malformed. On error, a suitable message is stored in the
  1033. <em class="parameter"><code>conn</code></em> object, whether or not <em class="parameter"><code>error</code></em> is <code class="symbol">NULL</code>.
  1034. </p><p>
  1035. <code class="function">PQescapeStringConn</code> returns the number of bytes written
  1036. to <em class="parameter"><code>to</code></em>, not including the terminating zero byte.
  1037. </p></dd><dt id="LIBPQ-PQESCAPESTRING"><span class="term">
  1038. <code class="function">PQescapeString</code>
  1039. <a id="id-1.7.3.10.6.3.4.1.2" class="indexterm"></a>
  1040. </span></dt><dd><p>
  1041. <code class="function">PQescapeString</code> is an older, deprecated version of
  1042. <code class="function">PQescapeStringConn</code>.
  1043. </p><pre class="synopsis">
  1044. size_t PQescapeString (char *to, const char *from, size_t length);
  1045. </pre><p>
  1046. </p><p>
  1047. The only difference from <code class="function">PQescapeStringConn</code> is that
  1048. <code class="function">PQescapeString</code> does not take <code class="structname">PGconn</code>
  1049. or <em class="parameter"><code>error</code></em> parameters.
  1050. Because of this, it cannot adjust its behavior depending on the
  1051. connection properties (such as character encoding) and therefore
  1052. <span class="emphasis"><em>it might give the wrong results</em></span>. Also, it has no way
  1053. to report error conditions.
  1054. </p><p>
  1055. <code class="function">PQescapeString</code> can be used safely in
  1056. client programs that work with only one <span class="productname">PostgreSQL</span>
  1057. connection at a time (in this case it can find out what it needs to
  1058. know <span class="quote">“<span class="quote">behind the scenes</span>”</span>). In other contexts it is a security
  1059. hazard and should be avoided in favor of
  1060. <code class="function">PQescapeStringConn</code>.
  1061. </p></dd><dt id="LIBPQ-PQESCAPEBYTEACONN"><span class="term">
  1062. <code class="function">PQescapeByteaConn</code>
  1063. <a id="id-1.7.3.10.6.3.5.1.2" class="indexterm"></a>
  1064. </span></dt><dd><p>
  1065. Escapes binary data for use within an SQL command with the type
  1066. <code class="type">bytea</code>. As with <code class="function">PQescapeStringConn</code>,
  1067. this is only used when inserting data directly into an SQL command string.
  1068. </p><pre class="synopsis">
  1069. unsigned char *PQescapeByteaConn(PGconn *conn,
  1070. const unsigned char *from,
  1071. size_t from_length,
  1072. size_t *to_length);
  1073. </pre><p>
  1074. </p><p>
  1075. Certain byte values must be escaped when used as part of a
  1076. <code class="type">bytea</code> literal in an <acronym class="acronym">SQL</acronym> statement.
  1077. <code class="function">PQescapeByteaConn</code> escapes bytes using
  1078. either hex encoding or backslash escaping. See <a class="xref" href="datatype-binary.html" title="8.4. Binary Data Types">Section 8.4</a> for more information.
  1079. </p><p>
  1080. The <em class="parameter"><code>from</code></em> parameter points to the first
  1081. byte of the string that is to be escaped, and the
  1082. <em class="parameter"><code>from_length</code></em> parameter gives the number of
  1083. bytes in this binary string. (A terminating zero byte is
  1084. neither necessary nor counted.) The <em class="parameter"><code>to_length</code></em>
  1085. parameter points to a variable that will hold the resultant
  1086. escaped string length. This result string length includes the terminating
  1087. zero byte of the result.
  1088. </p><p>
  1089. <code class="function">PQescapeByteaConn</code> returns an escaped version of the
  1090. <em class="parameter"><code>from</code></em> parameter binary string in memory
  1091. allocated with <code class="function">malloc()</code>. This memory should be freed using
  1092. <code class="function">PQfreemem()</code> when the result is no longer needed. The
  1093. return string has all special characters replaced so that they can
  1094. be properly processed by the <span class="productname">PostgreSQL</span>
  1095. string literal parser, and the <code class="type">bytea</code> input function. A
  1096. terminating zero byte is also added. The single quotes that must
  1097. surround <span class="productname">PostgreSQL</span> string literals are
  1098. not part of the result string.
  1099. </p><p>
  1100. On error, a null pointer is returned, and a suitable error message
  1101. is stored in the <em class="parameter"><code>conn</code></em> object. Currently, the only
  1102. possible error is insufficient memory for the result string.
  1103. </p></dd><dt id="LIBPQ-PQESCAPEBYTEA"><span class="term">
  1104. <code class="function">PQescapeBytea</code>
  1105. <a id="id-1.7.3.10.6.3.6.1.2" class="indexterm"></a>
  1106. </span></dt><dd><p>
  1107. <code class="function">PQescapeBytea</code> is an older, deprecated version of
  1108. <code class="function">PQescapeByteaConn</code>.
  1109. </p><pre class="synopsis">
  1110. unsigned char *PQescapeBytea(const unsigned char *from,
  1111. size_t from_length,
  1112. size_t *to_length);
  1113. </pre><p>
  1114. </p><p>
  1115. The only difference from <code class="function">PQescapeByteaConn</code> is that
  1116. <code class="function">PQescapeBytea</code> does not take a <code class="structname">PGconn</code>
  1117. parameter. Because of this, <code class="function">PQescapeBytea</code> can
  1118. only be used safely in client programs that use a single
  1119. <span class="productname">PostgreSQL</span> connection at a time (in this case
  1120. it can find out what it needs to know <span class="quote">“<span class="quote">behind the
  1121. scenes</span>”</span>). It <span class="emphasis"><em>might give the wrong results</em></span> if
  1122. used in programs that use multiple database connections (use
  1123. <code class="function">PQescapeByteaConn</code> in such cases).
  1124. </p></dd><dt id="LIBPQ-PQUNESCAPEBYTEA"><span class="term">
  1125. <code class="function">PQunescapeBytea</code>
  1126. <a id="id-1.7.3.10.6.3.7.1.2" class="indexterm"></a>
  1127. </span></dt><dd><p>
  1128. Converts a string representation of binary data into binary data
  1129. — the reverse of <code class="function">PQescapeBytea</code>. This
  1130. is needed when retrieving <code class="type">bytea</code> data in text format,
  1131. but not when retrieving it in binary format.
  1132. </p><pre class="synopsis">
  1133. unsigned char *PQunescapeBytea(const unsigned char *from, size_t *to_length);
  1134. </pre><p>
  1135. </p><p>
  1136. The <em class="parameter"><code>from</code></em> parameter points to a string
  1137. such as might be returned by <code class="function">PQgetvalue</code> when applied
  1138. to a <code class="type">bytea</code> column. <code class="function">PQunescapeBytea</code>
  1139. converts this string representation into its binary representation.
  1140. It returns a pointer to a buffer allocated with
  1141. <code class="function">malloc()</code>, or <code class="symbol">NULL</code> on error, and puts the size of
  1142. the buffer in <em class="parameter"><code>to_length</code></em>. The result must be
  1143. freed using <code class="function">PQfreemem</code> when it is no longer needed.
  1144. </p><p>
  1145. This conversion is not exactly the inverse of
  1146. <code class="function">PQescapeBytea</code>, because the string is not expected
  1147. to be <span class="quote">“<span class="quote">escaped</span>”</span> when received from <code class="function">PQgetvalue</code>.
  1148. In particular this means there is no need for string quoting considerations,
  1149. and so no need for a <code class="structname">PGconn</code> parameter.
  1150. </p></dd></dl></div></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="libpq-status.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-async.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">33.2. Connection Status Functions </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 33.4. Asynchronous Command Processing</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1