Connections¶
-
class
dbapix.connection.
Connection
(engine, con)[source]¶ A normalized connection to a database.
See the DB-API 2.0 specs for the minimal methods provided there.
Cursors and Convenience¶
-
Connection.
cursor
()[source]¶ Get a
Cursor
for this connection.cur = con.cursor() cur.execute('SELECT 1') assert next(cur)[0] == 1
-
Connection.
execute
(query, params=None)[source]¶ Create a cursor, and execute a query on it in one step.
- Returns
The created
Cursor
.
See also
Cursor.execute()
for parameters and examples.
-
Connection.
select
(*args, **kwargs)[source]¶ Pythonic wrapper for selecting.
See also
Cursor.select()
for parameters and examples.
-
Connection.
insert
(*args, **kwargs)[source]¶ Pythonic wrapper for inserting.
See also
Cursor.insert()
for parameters and examples.
-
Connection.
update
(*args, **kwargs)[source]¶ Pythonic wrapper for updating.
See also
Cursor.update()
for parameters and examples.
Transactions¶
Transaction management is one of the larger normalization efforts of dbapix.
Connections start in “autocommit” mode (and can be inspected by Connection.autocommit
).
When in autocommit, the connection behaves as if it automatically commits after every query; your changes are immediately reflected in the database and availible to other connections.
When not in autocommit, the connection ensures it is in a transaction before
executing a query, and the user must call Connection.commit()
for changes
to be reflected in the database (or may call Connection.rollback()
to rollback).
Regardless of the state of autocommit, you can enter a transaction by using
the connection as a context manager, or calling Connection.begin()
. Autocommit
is restored after the transaction if it was set before.
You can use a connection as a context manager to implicitly
begin()
/commit()
/rollback()
:
with con:
# Do stuff.
pass
or you can call the methods explicitly:
con.begin()
try:
# Do stuff.
pass
except:
con.rollback()
else:
con.commit()
-
Connection.
autocommit
¶ Is every query executed in an implicit transaction that is auto committed?
Defaults to
True
on new connections.
-
Connection.
begin
()[source]¶ Assert that we are or will be in a transaction.
- Returns
A context manager that will automatically rollback or commit the current transaction.
If it can be avoided, this does not actually start a transaction, but instead asserts that the connection is not in autocommit mode. If autocommit cannot be disabled, then an explicit
BEGIN
will be executed.commit()
androllback()
restore autocommit to it’s value whenbegin()
was called.
Session¶
-
Connection.
closed
¶ Has this connection been closed?
-
Connection.
close
()[source]¶ Close the connection immediately.
This prevents the engine from reusing connections, and so is discouraged unless you are sure the connection should not be reused.