Cursors¶
-
class
dbapix.cursor.
Cursor
(engine, raw)[source]¶ A database cursor, used to manage the context of a fetch operation.
See the DB-API 2.0 specs for the full specifications. Here we will document the basics and notable differences.
Executing¶
-
Cursor.
execute
(query, params=None, _stack_depth=0)[source]¶ Execute a query.
- Parameters
query (str) – The SQL to execute.
params – A
tuple
,dict
, orNone
._stack_depth (int) – How many steps up the callchain to pull f-string-style parameters from.
cur = con.execute('SELECT 1') assert next(cur)[0] == 1
See also
Parameters for a discussion of parameters binding.
Fetching Results¶
Direct¶
You can call a variety of methods to get your data back as Row
objects:
-
Cursor.
fetchone
()[source]¶ Fetch the next row of a query result set.
- Returns
A
Row
, orNone
when no more data is available.
Iteration¶
You can also treat the cursor as an iterator, e.g.:
for row in cur.execute('SELECT * FROM foo'):
# Do something with the row.
pass
Pandas DataFrame¶
-
Cursor.
as_dataframe
(rows=None, **kwargs)[source]¶ Fetch all (remaining) rows as a
pandas.DataFrame
.- Parameters
rows (list) – Manually picked rows to convert to a
DataFrame
.**kwargs – (e.g.
index
,exclude
,coerce_float
) to pass to pandas.DataFrame.from_records.
- Returns
pandas.DataFrame