[DB-SIG] Use of context managers with DB API 2
Christoph Zwerschke
cito at online.de
Sat Nov 3 23:50:34 CET 2012
Am 03.11.2012 20:56, schrieb M.-A. Lemburg:
> That said, using connections as context managers in the described
> way is popular and probably already a standard practice, so I
> guess it's better to standardize on it and document it properly,
> rather than leaving it open for interpretation - even if just to
> settle on one interpretation.
Yep. The fact that PySqlite is now part of the standard library has
already created a de facto standard, and most other database modules
seem to follow it.
>> Or, when using the shortcut methods:
>>
>> with dbapi2.connect(...) as con:
>> with con.transaction:
>> con.execute("insert into debit(amount) values (-100)")
>> con.execute("insert into credit(amount) values (100)")
>> with con.transaction:
>> con.execute("insert into debit(amount) values (-200)")
>> con.execute("insert into credit(amount) values (200)")
>
> The "shortcut" methods you describe were explicitly removed when
> moving from DB-API 1.0 to 2.0, so it's not a good idea to reintroduce
> them :-)
But PySqlite has already reintrocuced them. They are now also part of
the standard library:
http://docs.python.org/2/library/sqlite3.html#using-shortcut-methods
Is there anything bad about these shortcut methods?
-- Christoph
More information about the DB-SIG
mailing list