Database Reference
In-Depth Information
Using PQfnumber
The
PQfnumber
function returns the column number associated with the
column_name
argument. The syntax for
PQfnumber
is as follows:
int PQfnumber(const PGresult *res,
const char *column_name);
The following is a code snippet to explain the
PQfnumber
function:
ntuples = PQntuples(result);
for (i = 0; i < ntuples; i++)
{
char *id, *name;
/* Get the column number of column "id" and "name" */
int id_col = PQfnumber(result, "id");
int name_col = PQfnumber(result, "name");
id = PQgetvalue(result, i, id_col);
name = PQgetvalue(result, i, name_col);
fprintf ("Data: id = %s, name = %s\n", id, name);
}
Using PQftable
In complex queries where multiple tables are referenced, we need to know the table
OID from which that column is fetched. The
PQftable
function gets the OID of table
of that particular column in the result set.
The syntax for
PQftable
is as follows:
Oid PQftable(const PGresult *res,
int column_number);
The following is a code snippet to explain the
PQftable
function:
ntuples = PQntuples(result);
for (i = 0; i < ntuples; i++)
{
char *id, *name;
int id_col = PQfnumber(result, "id");
int name_col = PQfnumber(result, "name");
/* Get Table oid of column "id" */
int oid = PQftable(result, id_col);
fprintf(stdout, "Table oid is = %d \n", oid);
}