Results API¶
The QueryResult and Row classes represent query results and individual rows.
QueryResult¶
QueryResult contains the result of a query execution.
Methods¶
rows() -> List[Row]¶
Get all rows as a list.
result = await session.execute("SELECT * FROM users")
all_rows = result.rows()
for row in all_rows:
print(row.columns())
first_row() -> Optional[Row]¶
Get the first row, or None if empty.
result = await session.execute("SELECT * FROM users WHERE id = ?", {"id": 1})
row = result.first_row()
if row:
print(row.columns())
single_row() -> Row¶
Get the single row. Raises if not exactly one row.
try:
row = result.single_row()
print(row.columns())
except ValueError as e:
print(f"Expected one row: {e}")
Raises: ValueError if zero or multiple rows
first_row_typed() -> Optional[Dict]¶
Get the first row as a dictionary.
row_dict = result.first_row_typed()
if row_dict:
print(row_dict) # {"col_0": value, "col_1": value, ...}
rows_typed() -> List[Dict]¶
Get all rows as dictionaries.
col_specs() -> List[Dict]¶
Get column specifications.
tracing_id() -> Optional[str]¶
Get the trace ID if tracing was enabled.
warnings() -> List[str]¶
Get any warnings from the query.
Special Methods¶
__iter__¶
Iterate over rows.
__len__¶
Get number of rows.
__bool__¶
Check if result has rows.
Row¶
Row represents a single row from query results.
Methods¶
columns() -> List[Any]¶
Get all column values as a list.
as_dict() -> Dict[str, Any]¶
Convert row to dictionary.
get(index: int) -> Any¶
Get column value by index.
Raises: IndexError if index out of range
Special Methods¶
__getitem__¶
Access columns by index.
__len__¶
Get number of columns.
__repr__¶
String representation.
Usage Examples¶
Check if Exists¶
result = await session.execute(
"SELECT id FROM users WHERE id = ?",
{"id": 123}
)
if result.first_row():
print("User exists")
else:
print("User not found")
Get Single Value¶
result = await session.execute(
"SELECT COUNT(*) FROM users"
)
count = result.first_row()[0]
print(f"Total users: {count}")
Process All Rows¶
result = await session.execute("SELECT id, name, email FROM users")
for row in result:
id, name, email = row.columns()
print(f"{id}: {name} <{email}>")
Get as Dictionaries¶
result = await session.execute("SELECT * FROM users")
users = result.rows_typed()
for user in users:
print(user) # {"col_0": 1, "col_1": "Alice", ...}