Query Results

Results from calls to Session.query and Session.callproc are returned as an instance of the Results class. The Results class provides multiple ways to access the information about a query and the data returned from PostgreSQL.


The following examples illustrate the various behaviors that the Results class implements:

Using Results as an Iterator

for row in session.query('SELECT * FROM foo'):
    print row

Accessing an individual row by index

results = session.query('SELECT * FROM foo')
print results[1]  # Access the second row of the results

Casting single row results as a dict

results = session.query('SELECT * FROM foo LIMIT 1')
print results.as_dict()

Checking to see if a query was successful

results = session.query("UPDATE foo SET bar='baz' WHERE qux='corgie'")
if results:
    print 'Success'

Checking the number of rows by using len(Results)

results = session.query('SELECT * FROM foo')
print '%i rows' % len(results)

Class Documentation

class queries.Results(cursor)[source]

The Results class contains the results returned from Session.query and Session.callproc. It is able to act as an iterator and provides many different methods for accessing the information about and results from a query.

Parameters:cursor (psycopg2.extensions.cursor) – The cursor for the results

Return a single row result as a dictionary. If the results contain multiple rows, a ValueError will be raised.


Return the number of rows that were returned from the query

Return type:int

Used in asynchronous sessions for freeing results and their locked connections.


Return all of the rows that are in the result set.

Return type:list

Return a read-only value of the query that was submitted to PostgreSQL.

Return type:str

Return the current offset of the result set

Return type:int

Return the status message returned by PostgreSQL after the query was executed.

Return type:str