Re: try pattern for database connection with the close method

2015-02-22 Thread Mario Figueiredo
On Sat, 21 Feb 2015 16:22:36 +0100, Peter Otten <__pete...@web.de> wrote: > >Why would you care about a few lines? You don't repeat them, do you? Put the >code into a function or a context manager and invoke it with Thanks for the suggestions that followed. -- https://mail.python.org/mailman/li

Re: try pattern for database connection with the close method

2015-02-22 Thread Mario Figueiredo
On Sun, 22 Feb 2015 13:15:09 -0600, Skip Montanaro wrote: > >Sorry, I haven't paid careful attention to this thread, so perhaps >this has already been suggested, however... Can't you write your own >class which delegates to the necessary sqlite3 bits and has a context >manager with the desired be

Re: try pattern for database connection with the close method

2015-02-22 Thread Mario Figueiredo
On Sun, 22 Feb 2015 19:07:03 +, Mark Lawrence wrote: > >Looks like you're correct. Knock me down with a feather, Clevor Trevor. It took me by surprise when I first encountered it too. The rationale apparently is that the context manager is strictly a transactional feature, allowing for mult

Re: try pattern for database connection with the close method

2015-02-22 Thread Skip Montanaro
On Sun, Feb 22, 2015 at 12:41 PM, Mario Figueiredo wrote: > The sqlite context manager doesn't close a database connection on > exit. It only ensures, commits and rollbacks are performed. Sorry, I haven't paid careful attention to this thread, so perhaps this has already been suggested, however..

Re: try pattern for database connection with the close method

2015-02-22 Thread Mark Lawrence
On 22/02/2015 18:41, Mario Figueiredo wrote: On Sat, 21 Feb 2015 12:22:58 +, Mark Lawrence 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:

Re: try pattern for database connection with the close method

2015-02-22 Thread Mario Figueiredo
On Sat, 21 Feb 2015 12:22:58 +, Mark Lawrence 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 da

Re: try pattern for database connection with the close method

2015-02-21 Thread Peter Otten
Ian Kelly wrote: > On Sat, Feb 21, 2015 at 8:27 AM, Peter Otten <__pete...@web.de> wrote: >> Ian Kelly wrote: >> >>> On Sat, Feb 21, 2015 at 5:22 AM, Mark Lawrence >>> wrote: try: with lite.connect('data.db') as db: try: db.execute(sql, parms) except l

Re: try pattern for database connection with the close method

2015-02-21 Thread Ian Kelly
On Sat, Feb 21, 2015 at 8:27 AM, Peter Otten <__pete...@web.de> wrote: > Ian Kelly wrote: > >> On Sat, Feb 21, 2015 at 5:22 AM, Mark Lawrence >> wrote: >>> try: >>> with lite.connect('data.db') as db: >>> try: >>> db.execute(sql, parms) >>> except lite.IntegrityError: >>>

Re: try pattern for database connection with the close method

2015-02-21 Thread Peter Otten
Ian Kelly wrote: > On Sat, Feb 21, 2015 at 5:22 AM, Mark Lawrence > wrote: >> On 21/02/2015 02:42, Mario Figueiredo wrote: >>> >>> Hello all, >>> >>> I'm using the following pattern for db access that requires me to >>> close the connection as soon as it is not needed: >>> >>> import sq

Re: try pattern for database connection with the close method

2015-02-21 Thread Peter Otten
Mario Figueiredo wrote: > Hello all, > > I'm using the following pattern for db access that requires me to > close the connection as soon as it is not needed: > > import sqlite3 as lite > > try: > db = lite.connect('data.db') > except lite.DatabaseError: >

Re: try pattern for database connection with the close method

2015-02-21 Thread Ian Kelly
On Sat, Feb 21, 2015 at 5:22 AM, Mark Lawrence wrote: > On 21/02/2015 02:42, Mario Figueiredo wrote: >> >> Hello all, >> >> I'm using the following pattern for db access that requires me to >> close the connection as soon as it is not needed: >> >> import sqlite3 as lite >> >> t

Re: try pattern for database connection with the close method

2015-02-21 Thread Mark Lawrence
On 21/02/2015 02:42, Mario Figueiredo wrote: Hello all, I'm using the following pattern for db access that requires me to close the connection as soon as it is not needed: import sqlite3 as lite try: db = lite.connect('data.db') except lite.DatabaseErro

Re: try pattern for database connection with the close method

2015-02-20 Thread Chris Kaynor
On Fri, Feb 20, 2015 at 6:42 PM, Mario Figueiredo wrote: > import sqlite3 as lite > > try: > db = lite.connect('data.db') > except lite.DatabaseError: > raise OSError('database file corrupt or not found.') > else: > try: >

try pattern for database connection with the close method

2015-02-20 Thread Mario Figueiredo
Hello all, I'm using the following pattern for db access that requires me to close the connection as soon as it is not needed: import sqlite3 as lite try: db = lite.connect('data.db') except lite.DatabaseError: raise OSError('database file corrupt