Home Explore Blog CI



postgresql

61th chunk of `doc/src/sgml/libpq.sgml`
690196fcf8e5345d25e1561cb6ec4ef6c8da44c48c0910980000000100000ff5
 these functions will act as though the result has zero rows and zero columns.
   </para>

   <variablelist>
    <varlistentry id="libpq-PQntuples">
     <term><function>PQntuples</function><indexterm><primary>PQntuples</primary></indexterm></term>

     <listitem>
      <para>
       Returns the number of rows (tuples) in the query result.
       (Note that <structname>PGresult</structname> objects are limited to no more
       than <literal>INT_MAX</literal> rows, so an <type>int</type> result is
       sufficient.)

<synopsis>
int PQntuples(const PGresult *res);
</synopsis>

      </para>
     </listitem>
    </varlistentry>

    <varlistentry id="libpq-PQnfields">
     <term><function>PQnfields</function><indexterm><primary>PQnfields</primary></indexterm></term>

     <listitem>
      <para>
       Returns the number of columns (fields) in each row of the query
       result.

<synopsis>
int PQnfields(const PGresult *res);
</synopsis>
      </para>
     </listitem>
    </varlistentry>

    <varlistentry id="libpq-PQfname">
     <term><function>PQfname</function><indexterm><primary>PQfname</primary></indexterm></term>

     <listitem>
      <para>
       Returns the column name associated with the given column number.
       Column numbers start at 0. The caller should not free the result
       directly. It will be freed when the associated
       <structname>PGresult</structname> handle is passed to
       <xref linkend="libpq-PQclear"/>.
<synopsis>
char *PQfname(const PGresult *res,
              int column_number);
</synopsis>
      </para>

      <para>
       <symbol>NULL</symbol> is returned if the column number is out of range.
      </para>
     </listitem>
    </varlistentry>

    <varlistentry id="libpq-PQfnumber">
     <term><function>PQfnumber</function><indexterm><primary>PQfnumber</primary></indexterm></term>

     <listitem>
      <para>
       Returns the column number associated with the given column name.
<synopsis>
int PQfnumber(const PGresult *res,
              const char *column_name);
</synopsis>
      </para>

      <para>
       -1 is returned if the given name does not match any column.
      </para>

      <para>
       The given name is treated like an identifier in an SQL command,
       that is, it is downcased unless double-quoted.  For example, given
       a query result generated from the SQL command:
<programlisting>
SELECT 1 AS FOO, 2 AS "BAR";
</programlisting>
       we would have the results:
<programlisting>
PQfname(res, 0)              <lineannotation>foo</lineannotation>
PQfname(res, 1)              <lineannotation>BAR</lineannotation>
PQfnumber(res, "FOO")        <lineannotation>0</lineannotation>
PQfnumber(res, "foo")        <lineannotation>0</lineannotation>
PQfnumber(res, "BAR")        <lineannotation>-1</lineannotation>
PQfnumber(res, "\"BAR\"")    <lineannotation>1</lineannotation>
</programlisting>
      </para>
     </listitem>
    </varlistentry>

    <varlistentry id="libpq-PQftable">
     <term><function>PQftable</function><indexterm><primary>PQftable</primary></indexterm></term>

     <listitem>
      <para>
       Returns the OID of the table from which the given column was
       fetched.  Column numbers start at 0.
<synopsis>
Oid PQftable(const PGresult *res,
             int column_number);
</synopsis>
      </para>

      <para>
       <literal>InvalidOid</literal> is returned if the column number is out of range,
       or if the specified column is not a simple reference to a table column.
       You can query the system table <literal>pg_class</literal> to determine
       exactly which table is referenced.
      </para>

      <para>
       The type <type>Oid</type> and the constant
       <literal>InvalidOid</literal> will be defined when you include
       the <application>libpq</application> header file. They will both
       be some integer type.
      </para>
     </listitem>
    </varlistentry>

    <varlistentry id="libpq-PQftablecol">
     <term><function>PQftablecol</function><indexterm><primary>PQftablecol</primary></indexterm></term>

Title: Retrieving Column Information from Query Results in libpq
Summary
This section describes functions in libpq used to retrieve column-specific information from a query result, including `PQfname` (to get the column name by number), `PQfnumber` (to get the column number by name), `PQftable` (to get the OID of the table the column was fetched from). It also explains how column names are treated, including case sensitivity and quoting, and how to handle cases where a column is not a simple table reference.