.. note:: You can use :ref:`with conn.cursor(): ...<usage>`
to close the cursor automatically when the block is exited.
- .. automethod:: execute(query, params=None, prepare=None, row_factory=None) -> Cursor
+ .. automethod:: execute(query, params=None, prepare=None) -> Cursor
:param query: The query to execute.
:type query: `!str`, `!bytes`, or `sql.Composable`
:param prepare: Force (`!True`) or disallow (`!False`) preparation of
the query. By default (`!None`) prepare automatically. See
:ref:`prepared-statements`.
- :param row_factory: Optional row factory to form result's row. See
- :ref:`row-factories` for details.
- :type row_factory: `Callable[[Cursor], Callable[[Sequence[Any]], Any]]`
The cursor is what returned calling `cursor()` without parameters. The
parameters are passed to its `~Cursor.execute()` and the cursor is
.. note:: You can use ``async with conn.cursor() as cur: ...`` to
close the cursor automatically when the block is exited.
- .. automethod:: execute(query, params=None, prepare=None, row_factory=None) -> AsyncCursor
+ .. automethod:: execute(query, params=None, prepare=None) -> AsyncCursor
.. automethod:: commit
.. automethod:: rollback
return make_row
These can then be used by specifying a `row_factory` argument in
-`Connection.connect()`, `Connection.cursor()`, `Cursor.execute()` and
-`Connection.execute()` or by writting to `Connection.row_factory` attribute.
+`Connection.connect()`, `Connection.cursor()`, or by writing to
+`Connection.row_factory` attribute.
.. code:: python
params: Optional[Params] = None,
*,
prepare: Optional[bool] = None,
- row_factory: Optional[RowFactory] = _null_row_factory,
) -> Cursor:
"""Execute a query and return a cursor to read its results."""
- if row_factory is _null_row_factory:
- row_factory = self.row_factory
- cur = self.cursor(row_factory=row_factory)
+ cur = self.cursor()
return cur.execute(query, params, prepare=prepare)
def commit(self) -> None:
params: Optional[Params] = None,
*,
prepare: Optional[bool] = None,
- row_factory: Optional[RowFactory] = _null_row_factory,
) -> AsyncCursor:
- if row_factory is _null_row_factory:
- row_factory = self.row_factory
- cur = self.cursor(row_factory=row_factory)
+ cur = self.cursor()
return await cur.execute(query, params, prepare=prepare)
async def commit(self) -> None:
cur = conn.execute("select 12, 22")
assert cur.fetchone() == (12, 22)
- cur = conn.execute("select 1, 2, 1, 2", row_factory=lambda cur: set)
- assert cur.fetchone() == {1, 2}
-
def test_row_factory(dsn):
conn = Connection.connect(dsn, row_factory=my_row_factory)
cur = conn.execute("select 'a' as ve")
assert cur.fetchone() == ["Ave"]
- cur = conn.execute("select 'a' as ve", row_factory=None)
- assert cur.fetchone() == ("a",)
-
with conn.cursor(row_factory=lambda c: set) as cur:
cur.execute("select 1, 1, 2")
assert cur.fetchall() == [{1, 2}]
cur = await aconn.execute("select 12, 22")
assert await cur.fetchone() == (12, 22)
- cur = await aconn.execute("select 1, 2, 1, 2", row_factory=lambda cur: set)
- assert await cur.fetchone() == {1, 2}
-
async def test_row_factory(dsn):
conn = await AsyncConnection.connect(dsn, row_factory=my_row_factory)
cur = await conn.execute("select 'a' as ve")
assert await cur.fetchone() == ["Ave"]
- cur = await conn.execute("select 'a' as ve", row_factory=None)
- assert await cur.fetchone() == ("a",)
-
async with conn.cursor(row_factory=lambda c: set) as cur:
await cur.execute("select 1, 1, 2")
assert await cur.fetchall() == [{1, 2}]