Re: Context manager support for cursors

Federico Di Gregorio <[email protected]> Tue, 24 Jul 2018 09:37:27 +0200
Newsgroups gmane.comp.python.db
Organization DNDG srl
Message-ID <[email protected]>
On 06/21/2018 01:31 AM, Gerald Venzl wrote:
> Hi all,
> 
> I was wondering whether there has been any particular reason that the 
> specification of the Cursor object doesn’t implement a context manager 
> to enable for example the “with” statement?
> Being able to use the “with” statement for database cursors, just like 
> other external resources like files, etc., I think makes a lot of sense:
> 
> with conn.cursor()as c:
>      c.execute("SELECT 'test' from dual")
>      result = c.fetchall()
>      print(result)

It does (make sense). The explanation is that context managers did not 
exist when the PEP was written. An update is long overdue, IMHO.

federico
_______________________________________________
DB-SIG maillist  -  [email protected]
https://mail.python.org/mailman/listinfo/db-sig