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, or None.

  • _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, or None when no more data is available.

Cursor.fetchmany(size=None)[source]

Fetch the next set of rows of a query result set.

Parameters

size (int) – Upper limit of rows to fetch; defaults to Cursor.arraysize.

Returns

A RowList of zero or more Row.

Cursor.fetchall()[source]

Fetch all (remaining) rows of a query result set.

Returns

A RowList of zero or more Row.

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

Query Builders

Cursor.select(table_name, fields, where=None, where_params=(), _stack_depth=0)[source]
Cursor.insert(table_name, data, returning=None)[source]
Cursor.update(table_name, data, where, where_params=(), _stack_depth=0)[source]

Wrapped

Cursor.wrapped

This is the underlying DB-API2 cursor object, in case you need to access it.

Cursor.__getattr__(key)[source]

Attributes that are not provided by dbapix are passed through to the wrapped cursor.