📄 libpq.sgml
字号:
condition.</para><para><variablelist><varlistentry><term><function>PQexecParams</function><indexterm><primary>PQexecParams</></></term><listitem><para> Submits a command to the server and waits for the result, with the ability to pass parameters separately from the SQL command text.<synopsis>PGresult *PQexecParams(PGconn *conn, const char *command, int nParams, const Oid *paramTypes, const char * const *paramValues, const int *paramLengths, const int *paramFormats, int resultFormat);</synopsis></para><para><function>PQexecParams</> is like <function>PQexec</>, but offers additionalfunctionality: parameter values can be specified separately from the commandstring proper, and query results can be requested in either text or binaryformat. <function>PQexecParams</> is supported only in protocol 3.0 and laterconnections; it will fail when using protocol 2.0.</para><para>If parameters are used, they are referred to in the command stringas <literal>$1</>, <literal>$2</>, etc.<parameter>nParams</> is the number of parameters supplied; it is the lengthof the arrays <parameter>paramTypes[]</>, <parameter>paramValues[]</>,<parameter>paramLengths[]</>, and <parameter>paramFormats[]</>. (Thearray pointers may be <symbol>NULL</symbol> when <parameter>nParams</> is zero.)<parameter>paramTypes[]</> specifies, by OID, the data types to be assigned tothe parameter symbols. If <parameter>paramTypes</> is <symbol>NULL</symbol>, or any particularelement in the array is zero, the server assigns a data type to the parametersymbol in the same way it would do for an untyped literal string.<parameter>paramValues[]</> specifies the actual values of the parameters.A null pointer in this array means the corresponding parameter is null;otherwise the pointer points to a zero-terminated text string (for textformat) or binary data in the format expected by the server (for binaryformat).<parameter>paramLengths[]</> specifies the actual data lengths ofbinary-format parameters. It is ignored for null parameters and text-formatparameters. The array pointer may be null when there are no binaryparameters.<parameter>paramFormats[]</> specifies whether parameters are text (put a zeroin the array) or binary (put a one in the array). If the array pointer isnull then all parameters are presumed to be text.<parameter>resultFormat</> is zero to obtain results in text format, or one toobtain results in binary format. (There is not currently a provision toobtain different result columns in different formats, although that ispossible in the underlying protocol.)</para></listitem></varlistentry></variablelist>The primary advantage of <function>PQexecParams</> over <function>PQexec</>is that parameter values may be separated from the command string, thusavoiding the need for tedious and error-prone quoting and escaping.Unlike <function>PQexec</>, <function>PQexecParams</> allows at most one SQLcommand in the given string. (There can be semicolons in it, but not morethan one nonempty command.) This is a limitation of the underlying protocol,but has some usefulness as an extra defense against SQL-injection attacks.</para><tip><para>Specifying parameter types via OIDs is tedious, particularly if you prefernot to hard-wire particular OID values into your program. However, you canavoid doing so even in cases where the server by itself cannot determine thetype of the parameter, or chooses a different type than you want. In theSQL command text, attach an explicit cast to the parameter symbol to show whatdata type you will send. For example,<programlisting>select * from mytable where x = $1::bigint;</programlisting>This forces parameter <literal>$1</> to be treated as <type>bigint</>, whereasby default it would be assigned the same type as <literal>x</>. Forcing theparameter type decision, either this way or by specifying a numeric type OID,is strongly recommended when sending parameter values in binary format, becausebinary format has less redundancy than text format and so there is less chancethat the server will detect a type mismatch mistake for you.</para></tip><para><variablelist><varlistentry><term><function>PQprepare</function><indexterm><primary>PQprepare</></></term><listitem><para> Submits a request to create a prepared statement with the given parameters, and waits for completion.<synopsis>PGresult *PQprepare(PGconn *conn, const char *stmtName, const char *query, int nParams, const Oid *paramTypes);</synopsis></para><para><function>PQprepare</> creates a prepared statement for later execution with<function>PQexecPrepared</>.This feature allows commandsthat will be used repeatedly to be parsed and planned just once, ratherthan each time they are executed.<function>PQprepare</> is supported only in protocol 3.0 and laterconnections; it will fail when using protocol 2.0.</para><para>The function creates a prepared statement named <parameter>stmtName</>from the <parameter>query</> string, which must contain a single SQL command.<parameter>stmtName</> may be <literal>""</> to create an unnamed statement,in which case any pre-existing unnamed statement is automatically replaced;otherwise it is an error if the statement name is already defined in thecurrent session.If any parameters are used, they are referredto in the query as <literal>$1</>, <literal>$2</>, etc.<parameter>nParams</> is the number of parameters for which types arepre-specified in the array <parameter>paramTypes[]</>. (The array pointermay be <symbol>NULL</symbol> when <parameter>nParams</> is zero.)<parameter>paramTypes[]</> specifies, by OID, the data types to be assigned tothe parameter symbols. If <parameter>paramTypes</> is <symbol>NULL</symbol>,or any particular element in the array is zero, the server assigns a data typeto the parameter symbol in the same way it would do for an untyped literalstring. Also, the query may use parameter symbols with numbers higher than<parameter>nParams</>; data types will be inferred for these symbols aswell.</para><para>As with <function>PQexec</>, the result is normally a<structname>PGresult</structname> object whose contents indicate server-sidesuccess or failure. A null result indicates out-of-memory or inability tosend the command at all.Use <function>PQerrorMessage</function> to get more informationabout such errors.</para><para>At present, there is no way to determine the actual data type inferred forany parameters whose types are not specified in <parameter>paramTypes[]</>.This is a <application>libpq</> omission that will probably be rectifiedin a future release.</para></listitem></varlistentry></variablelist>Prepared statements for use with <function>PQexecPrepared</> can alsobe created by executing SQL <xref linkend="sql-prepare"endterm="sql-prepare-title"> statements. (But <function>PQprepare</>is more flexible since it does not require parameter types to bepre-specified.) Also, although there is no <application>libpq</>function for deleting a prepared statement, the SQL <xreflinkend="sql-deallocate" endterm="sql-deallocate-title"> statement canbe used for that purpose.</para><para><variablelist><varlistentry><term><function>PQexecPrepared</function><indexterm><primary>PQexecPrepared</></></term><listitem><para> Sends a request to execute a prepared statement with given parameters, and waits for the result.<synopsis>PGresult *PQexecPrepared(PGconn *conn, const char *stmtName, int nParams, const char * const *paramValues, const int *paramLengths, const int *paramFormats, int resultFormat);</synopsis></para><para><function>PQexecPrepared</> is like <function>PQexecParams</>, but thecommand to be executed is specified by naming a previously-preparedstatement, instead of giving a query string.This feature allows commandsthat will be used repeatedly to be parsed and planned just once, ratherthan each time they are executed.The statement must have been prepared previously in the current session.<function>PQexecPrepared</> is supported only in protocol 3.0 and laterconnections; it will fail when using protocol 2.0.</para><para>The parameters are identical to <function>PQexecParams</>, except that thename of a prepared statement is given instead of a query string, and the<parameter>paramTypes[]</> parameter is not present (it is not needed sincethe prepared statement's parameter types were determined when it was created).</para></listitem></varlistentry></variablelist></para><para>The<structname>PGresult</structname><indexterm><primary>PGresult</></>structure encapsulates the result returned by the server.<application>libpq</application> application programmers should becareful to maintain the <structname>PGresult</structname> abstraction.Use the accessor functions below to get at the contents of<structname>PGresult</structname>. Avoid directly referencing thefields of the <structname>PGresult</structname> structure because theyare subject to change in the future.<variablelist><varlistentry><term><function>PQresultStatus</function><indexterm><primary>PQresultStatus</></></term><listitem><para> Returns the result status of the command.<synopsis>ExecStatusType PQresultStatus(const PGresult *res);</synopsis></para><para><function>PQresultStatus</function> can return one of the following values:<variablelist> <varlistentry> <term><literal>PGRES_EMPTY_QUERY</literal></term> <listitem> <para>The string sent to the server was empty.</para> </listitem> </varlistentry> <varlistentry> <term><literal>PGRES_COMMAND_OK</literal></term> <listitem> <para>Successful completion of a command returning no data.</para> </listitem> </varlistentry> <varlistentry> <term><literal>PGRES_TUPLES_OK</literal></term> <listitem> <para>Successful completion of a command returning data (such as a <command>SELECT</> or <command>SHOW</>).</para> </listitem> </varlistentry> <varlistentry> <term><literal>PGRES_COPY_OUT</literal></term> <listitem> <para>Copy Out (from server) data transfer started.</para> </listitem> </varlistentry> <varlistentry> <term><literal>PGRES_COPY_IN</literal></term> <listitem> <para>Copy In (to server) data transfer started.</para> </listitem> </varlistentry> <varlistentry> <term><literal>PGRES_BAD_RESPONSE</literal></term> <listitem> <para>The server's response was not understood.</para> </listitem> </varlistentry> <varlistentry> <term><literal>PGRES_NONFATAL_ERROR</literal></term> <listitem> <para>A nonfatal error (a notice or warning) occurred.</para> </listitem> </varlistentry> <varlistentry> <term><literal>PGRES_FATAL_ERROR</literal></term> <listitem> <para>A fatal error occurred.</para> </listitem> </varlistentry></variablelist>If the result status is <literal>PGRES_TUPLES_OK</literal>, then thefunctions described below can be used to retrieve the rows returned bythe query. Note that a <command>SELECT</command> command that happensto retrieve zero rows still shows <literal>PGRES_TUPLES_OK</literal>.<literal>PGRES_COMMAND_OK</literal> is for commands that can neverreturn rows (<command>INSERT</command>, <command>UPDATE</command>,etc.). A response of <literal>PGRES_EMPTY_QUERY</literal> may indicatea bug in the client software.</para><para>A result of status <symbol>PGRES_NONFATAL_ERROR</symbol> will never bereturned directly by <function>PQexec</function> or other queryexecution functions; results of this kind are instead passed to the noticeprocessor (see <xref linkend="libpq-notice-processing">).</para></listitem></varlistentry><varlistentry><term><function>PQresStatus</function><indexterm><primary>PQresStatus</></></term><listitem><para> Converts the enumerated type returned by <function>PQresultStatus</> into a string constant describing the status code. The caller should not free the result.<synopsis>char *PQresStatus(ExecStatusType status);</synopsis></para></listitem></varlistentry><varlistentry><term><function>PQresultErrorMessage</function><indexterm><primary>PQresultErrorMessage</></></term><listitem><para>Returns the error message associated with the command, or an empty stringif there was no error.<synopsis>char *PQresultErrorMessage(const PGresult *res);</synopsis>If there was an error, the returned string will include a trailing newline. The caller should not free the result directly. It will be freed when the associated <structname>PGresult</> handle is passed to <function>PQclear</function>.</para><para>Immediately following a <function>PQexec</function> or <function>PQgetResult</function>call, <function>PQerrorMessage</function> (on the connection) will return the samestring as <function>PQresultErrorMessage</function> (on the result). However, a<structname>PGresult</structname> will retain its error messageuntil destroyed, whereas the connection's error message will change whensubsequent operations are done. Use <function>PQresultErrorMessage</function> when you want toknow the status associated with a particular <structname>PGresult</structname>; use <function>PQerrorMessage</function>when you want to know the status from the latest operation on the connection.</para></listitem></varlistentry><varlistentry><term><function>PQresultErrorField</function><indexterm><primary>PQresultErrorField</></></term><listitem><para>Returns an individual field of an error report.<synopsis>char *PQresultErrorField(const PGresult *res, int fieldcode);</synopsis><parameter>fieldcode</> is an error field identifier; see the symbolslisted below. <symbol>NULL</symbol> is returned if the<structname>PGresult</structname> is not an error or warning result,or does not include the specified field. Field values will normallynot include a trailing newline. The caller should not free the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -