[DB-SIG] Proposed DB-API extensions

Gerhard Häring gh at ghaering.de
Wed Mar 19 13:23:26 CET 2008


I'd like to propose the attached changes to the DB-API spec. My main 
motivation is to make DB-API modules less cumbersome to use when used 
directly.

If you want to try it out live, everything except .fetchscalar() is 
already implemented in pysqlite. The __enter__/__exit__ thing is in 
pysqlite and cx_Oracle.


==> .execute(), .executemany() returning self.

Ability to write shorter code. No more:

cur.execute("select ...")
for row in cur:
   ...

instead:

for row in cur.exeucte("..."):
   ...

==> .execute(), .executemany() in connection object.

They should have been placed here anyway. *cough* Users shouldn't have 
to mess with cursor objects normally. Ability to write shorter code:

con = module.connect(...)
for row in con.execute("..."):
   ...

==> __enter__ and __exit__ in the connection object.

Ability to automatically wrap database code in transactions, when using 
Python 2.5 or higher:

with con:
     con.execute(DML1)
     con.execute(DML2)

no more:

try:
    cur = con.cursor()
    cur.execute(DML1)
    cur.execute(DML2)
    con.commit()
except:
     con.rollback()

==> fetchscalar method in cursor object

This one is low-prio for me, but it's a common use case to only query a 
scalar value.

-- Gerhard
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: db_api_extensions.diff
Url: http://mail.python.org/pipermail/db-sig/attachments/20080319/181689dc/attachment.txt 


More information about the DB-SIG mailing list