6.9.1.5.4 Iterating a Query Result Set
You can iterate your query result set using the methods in
PgqlResultSet
.
You can position the cursor for iterating your query result set using the following methods:
first() : boolean
next() : boolean
previous() : boolean
last() : boolean
before_first()
after_last()
absolute(target_row_value) : boolean
relative(offset_value) : boolean
fetchone(): Tuple
fetchmany(no_of_rows): List of tuples
fetchall(): List of tuples
Once the cursor is positioned at the desired row, you can use the following getters to obtain values:
get(column_idx) : Object
get(column_name) : Object
get_boolean(column_idx) : boolean
get_boolean(column_name) : boolean
get_date(column_idx) : datetime.date
get_date(column_name) : datetime.date
get_float(column_idx) : Float
get_float(column_name) : Float
get_integer(column_idx) : Integer
get_integer(column_name) : Integer
get_list(column_idx) : List
get_list(column_name) : List
get_string(column_idx) : String
get_string(column_name) : String
get_time(column_idx) : datetime.time
get_time(column_name) : datetime.time
get_time_with_timezone(column_idx) : datetime.time
get_time_with_timezone(column_name) : datetime.time
get_timestamp(column_idx) : datetime.datetime
get_timestamp(column_name) : datetime.datetime
get_timestamp_with_timezone(column_idx) : datetime.datetime
get_timestamp_with_timezone(column_name) : datetime.datetime
get_value_type(column_idx) : Integer
get_value_type(column_name) : Integer
get_vertex_labels(column_idx) : List
get_vertex_labels(column_name) : List
See Retrieving PGQL-on-RDBMS results documentation for more information.
The following code samples illustrate cursor operations for iterating a result set using few of the cursor position and getter methods. These examples reference the query result set obtained in the example in the previous section.
# Call first() and retrieve value for "FROM_ACCT_ID"
>>> pgql_result_set.first()
True
>>> pgql_result_set.get("FROM_ACCT_ID")
781.0
# Call next() and retrieve value for "FROM_ACCT_ID"
>>> pgql_result_set.next()
True
>>> pgql_result_set.get("FROM_ACCT_ID")
978.0
# Call last() and retrieve value for "FROM_ACCT_ID"
>>> pgql_result_set.last()
True
>>> pgql_result_set.get("FROM_ACCT_ID")
842.0
# Call previous() and retrieve value for "FROM_ACCT_ID"
>>> pgql_result_set.previous()
True
>>> pgql_result_set.get("FROM_ACCT_ID")
838.0
# Reset the result set and offset by 6. Then retrieve value for "FROM_ACCT_ID"
>>> pgql_result_set.before_first()
>>> pgql_result_set.relative(6)
True
>>> pgql_result_set.get("FROM_ACCT_ID")
925.0
# Reach the end of the result set and offset by -2. Then retrieve value for "FROM_ACCT_ID"
>>> pgql_result_set.after_last()
>>> pgql_result_set.relative(-2)
True
>>> pgql_result_set.get("FROM_ACCT_ID")
838.0
# Call absolute() and provide an absolute row value. Then retrieve value for "FROM_ACCT_ID"
>>> pgql_result_set.absolute(3)
True
>>> pgql_result_set.get_float("FROM_ACCT_ID")
900.0
# Get a specific row or a set of rows
>>> pgql_result_set.get_slice(0,1)
[781.0, 712.0, 1000.0]
>>> pgql_result_set.get_row(0)
[781.0, 712.0, 1000.0]
Iterating a Result Set Using the Python Index Operator
You can also iterate through the query result set using the Python index operator as shown:
# Retrieving a value from a tuple
>>> pgql_result_set[4, "double", "FROM_ACCT_ID"]
907.0
# Retrieving a value using index value
>>> pgql_result_set[4].get("FROM_ACCT_ID")
907.0
# Fetch a row or a set of rows
>>> pgql_result_set[0:1]
[781.0, 712.0, 1000.0]
Iterating a Result Set Using a Python loop
Optionally, you can also iterate through the query result set using a Python loop. For example:
# Using the result set as an iterator of a for loop
>>> for result in pgql_result_set:
print(result)
[781.0, 712.0, 1000.0]
[190.0, 555.0, 1000.0]
[191.0, 329.0, 1000.0]
[198.8. 57.0, 1000.0]
[220.0, 441.0, 1000.0]
[251.0, 387.0, 1000.0]
[254.0, 188.0, 1000.0]
[259.0, 305.0, 1000.0]
[261.0, 145.0, 1000.0]
[263.0, 40.0, 1000.0]
Iterating a Result Set Using Fetch Methods
You can iterate through the query result set and fetch rows using the fetch methods. For example:
# Using the fetch methods to fetch rows from the result set
>>> pgql_result_set.fetchone()
(781.0, 712.0, 1000.0)
>>> pgql_result_set.fetchmany(4)
[(190.0, 555.0, 1000.0), (191.0, 329.0, 1000.0), (198.8. 57.0, 1000.0), (220.0, 441.0, 1000.0)]
>>> pgql_result_set.fetchall()
[(251.0, 387.0, 1000.0), (254.0, 188.0, 1000.0), (259.0, 305.0, 1000.0), (261.0, 145.0, 1000.0), (263.0, 40.0, 1000.0)]
Parent topic: Using the Python Client to Execute PGQL Queries