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.
Examples¶
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 fromSession.query
andSession.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 -
as_dict
()[source]¶ Return a single row result as a dictionary. If the results contain multiple rows, a
ValueError
will be raised.Returns: dict Raises: ValueError
-
query
¶ Return a read-only value of the query that was submitted to PostgreSQL.
Return type: str
-
rownumber
¶ Return the current offset of the result set
Return type: int
-
status
¶ Return the status message returned by PostgreSQL after the query was executed.
Return type: str
-