PGdatabase::exec submits a query to Postgres and returns result status. In case of an error PGdatabase::errormessage can be used to get more information on the error.
void ExecStatusType PGdatabase::exec(char *query);The following status results can be expected:
PGRES_EMPTY_QUERY |
PGRES_COMMAND_OK, if the query was a command |
PGRES_TUPLES_OK, if the query successfully returned tuples |
PGRES_COPY_OUT |
PGRES_COPY_IN |
PGRES_BAD_RESPONSE, if an unexpected response was received |
PGRES_NONFATAL_ERROR |
PGRES_FATAL_ERROR |
If the result status is PGRES_TUPLES_OK, then the following routines can be used to retrieve the tuples returned by the query.
PGdatabase::ntuples returns the number of tuples (instances) in the query result.
int PGdatabase::ntuples()
PGdatabase::nfields returns the number of fields (attributes) in the query result.
int PGdatabase::nfields()
PGdatabase::fieldname returns the field (attribute) name associated with the given field index. Field indices start at zero.
char* PGdatabase::fieldname(int field_index)
PGdatabase::fieldnum returns the field (attribute) index associated with the given field name.
int PGdatabase::fieldnum(char* field_name)
PGdatabase::fieldtype returns the field type of associated with the given field index or name. The integer returned is an internal coding of the type. Field indices start at zero.
Oid PGdatabase::fieldtype(int field_index) Oid PGdatabase::fieldtype(char* field_name)
PGdatabase::fieldsize returns the size in bytes of the field associated with the given field index or name. If the size returned is -1, the field is a variable length field. Field indices start at zero.
int2 PGdatabase::fieldsize(int field_index) int2 PGdatabase::fieldsize(char* field_name)
PGdatabase::getvalue returns the field (attribute) value. For most queries, the values returned by PGdatabase::getvalue is a null-terminated ASCII string representation of the attribute value. If the query was a result of a BINARY cursor, then the values returned by PGdatabase::getvalue is the binary representation of the type in the internal format of the backend server. It is the programmer's responsibility to cast and convert the data to the correct C++ type. The value return by PGdatabase::getvalue points to storage that is part of the PGdatabase structure. One must explicitly copy the value into other storage if it is to be used past the next query.
char* PGdatabase::getvalue(int tup_num, int field_index) char* PGdatabase::getvalue(int tup_num, char* field_name)
PGdatabase::getlength returns the length of a field (attribute) in bytes. If the field is a struct varlena, the length returned here does not include the size field of the varlena, i.e., it is 4 bytes less.
int PGdatabase::getlength(int tup_num, int field_index) int PGdatabase::getlength(int tup_num, char* field_name)
PGdatabase::printtuples prints out all the tuples and, optionally, the attribute names to the specified output stream.
void PGdatabase::printtuples( FILE* fout, /* output stream */ int printAttName,/* print attribute names or not*/ int terseOutput, /* delimiter bars or not?*/ int width /* width of column, variable width if 0*/ );