Unsupported versions: 7.2 / 7.1
This documentation is for an unsupported version of PostgreSQL.
You may want to view the same page for the current version, or one of the other supported versions listed above instead.

3.4. Query Execution Functions

3.4.1. Main Routines

  • Exec Sends a query to the backend server. It's probably more desirable to use one of the next two functions.

           ExecStatusType PgConnection::Exec(const char* query)
           
    
    Returns the result of the 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
  • ExecCommandOk Sends a command query to the backend server.

           int PgConnection::ExecCommandOk(const char *query)
           
    
    Returns TRUE if the command query succeeds.
  • ExecTuplesOk Sends a command query to the backend server.

           int PgConnection::ExecTuplesOk(const char *query)
           
    
    Returns TRUE if the command query succeeds.
  • ErrorMessage Returns the last error message text.

           const char *PgConnection::ErrorMessage()
           
    

3.4.2. Retrieving SELECT Result Information

  • Tuples Returns the number of tuples (rows) in the query result.

           int PgDatabase::Tuples()
           
    
  • Fields Returns the number of fields (attributes) in each tuple of the query result.

           int PgDatabase::Fields()
           
    
  • FieldName Returns the field (attribute) name associated with the given field index. Field indices start at 0.

           const char *PgDatabase::FieldName(int field_num)
           
    
  • FieldNum PQfnumber Returns the field (attribute) index associated with the given field name.

           int PgDatabase::FieldNum(const char* field_name)
           
    
    -1 is returned if the given name does not match any field.
  • FieldType Returns the field type associated with the given field index. The integer returned is an internal coding of the type. Field indices start at 0.

           Oid PgDatabase::FieldType(int field_num)
           
    
  • FieldType Returns the field type associated with the given field name. The integer returned is an internal coding of the type. Field indices start at 0.

           Oid PgDatabase::FieldType(const char* field_name)
           
    
  • FieldSize Returns the size in bytes of the field associated with the given field index. Field indices start at 0.

           short PgDatabase::FieldSize(int field_num)
           
    
    Returns the space allocated for this field in a database tuple given the field number. In other words the size of the server's binary representation of the data type. -1 is returned if the field is variable size.
  • FieldSize Returns the size in bytes of the field associated with the given field index. Field indices start at 0.

           short PgDatabase::FieldSize(const char *field_name)
           
    
    Returns the space allocated for this field in a database tuple given the field name. In other words the size of the server's binary representation of the data type. -1 is returned if the field is variable size.

3.4.3. Retrieving SELECT Result Values

  • GetValue Returns a single field (attribute) value of one tuple of a PGresult. Tuple and field indices start at 0.

           const char *PgDatabase::GetValue(int tup_num, int field_num)
           
    
    For most queries, the value returned by GetValue is a null-terminated ASCII string representation of the attribute value. But if BinaryTuples() is TRUE, the value returned by GetValue is the binary representation of the type in the internal format of the backend server (but not including the size word, if the field is variable-length). It is then the programmer's responsibility to cast and convert the data to the correct C type. The pointer returned by GetValue points to storage that is part of the PGresult structure. One should not modify it, and one must explicitly copy the value into other storage if it is to be used past the lifetime of the PGresult structure itself. BinaryTuples() is not yet implemented.
  • GetValue Returns a single field (attribute) value of one tuple of a PGresult. Tuple and field indices start at 0.

           const char *PgDatabase::GetValue(int tup_num, const char *field_name)
           
    
    For most queries, the value returned by GetValue is a null-terminated ASCII string representation of the attribute value. But if BinaryTuples() is TRUE, the value returned by GetValue is the binary representation of the type in the internal format of the backend server (but not including the size word, if the field is variable-length). It is then the programmer's responsibility to cast and convert the data to the correct C type. The pointer returned by GetValue points to storage that is part of the PGresult structure. One should not modify it, and one must explicitly copy the value into other storage if it is to be used past the lifetime of the PGresult structure itself. BinaryTuples() is not yet implemented.
  • GetLength Returns the length of a field (attribute) in bytes. Tuple and field indices start at 0.

           int PgDatabase::GetLength(int tup_num, int field_num)
           
    
    This is the actual data length for the particular data value, that is the size of the object pointed to by GetValue. Note that for ASCII-represented values, this size has little to do with the binary size reported by PQfsize.
  • GetLength Returns the length of a field (attribute) in bytes. Tuple and field indices start at 0.

           int PgDatabase::GetLength(int tup_num, const char* field_name)
           
    
    This is the actual data length for the particular data value, that is the size of the object pointed to by GetValue. Note that for ASCII-represented values, this size has little to do with the binary size reported by PQfsize.
  • DisplayTuples Prints out all the tuples and, optionally, the attribute names to the specified output stream.

           void PgDatabase::DisplayTuples(FILE *out = 0, int fillAlign = 1, 
            const char* fieldSep = "|",int printHeader = 1, int quiet = 0)
           
    
  • PrintTuples Prints out all the tuples and, optionally, the attribute names to the specified output stream.

           void PgDatabase::PrintTuples(FILE *out = 0, int printAttName = 1, 
            int terseOutput = 0, int width = 0) 
           
    

3.4.4. Retrieving Non-SELECT Result Information

  • CmdTuples Returns the number of rows affected after an INSERT, UPDATE or DELETE. If the command was anything else, it returns -1.

          int PgDatabase::CmdTuples()
           
    
  • OidStatus

           const char *PgDatabase::OidStatus()
           
    

3.4.5. Handling COPY Queries

  • GetLine

           int PgDatabase::GetLine(char* string, int length)
           
    
  • PutLine

           void PgDatabase::PutLine(const char* string)
           
    
  • EndCopy

           int PgDatabase::EndCopy()