try pattern for database connection with the close method

Mario Figueiredo marfig at gmail.com
Sun Feb 22 13:41:44 EST 2015


On Sat, 21 Feb 2015 12:22:58 +0000, Mark Lawrence
<breamoreboy at yahoo.co.uk> wrote:

>
>Use your context manager at the outer level.
>
>import sqlite3 as lite
>
>try:
>     with lite.connect('data.db') as db:
>     try:
>         db.execute(sql, parms)
>     except lite.IntegrityError:
>         raise ValueError('invalid data')
>except lite.DatabaseError:
>     raise OSError('database file corrupt or not found.')

The sqlite context manager doesn't close a database connection on
exit. It only ensures, commits and rollbacks are performed.



More information about the Python-list mailing list