Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'from:addr:yahoo.co.uk': 0.04; 'context': 0.07; 'finally:': 0.07; 'lawrence': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:method': 0.09; 'try:': 0.09; 'api': 0.11; 'language.': 0.14; 'clauses.': 0.16; 'corrupt': 0.16; 'lite': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'exception': 0.16; 'language': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'bit': 0.19; 'import': 0.22; 'header:User-Agent:1': 0.23; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'raise': 0.29; "i'm": 0.30; 'away.': 0.31; 'way?': 0.31; 'file': 0.32; 'level.': 0.33; 'subject:the': 0.34; 'subject:with': 0.35; 'connection': 0.35; 'except': 0.35; 'there': 0.35; 'manager': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'soon': 0.63; 'our': 0.64; 'charset:windows-1252': 0.65; 'note:': 0.66; 'close': 0.67; 'needed:': 0.84; 'subject:try': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: try pattern for database connection with the close method Date: Sat, 21 Feb 2015 12:22:58 +0000 References: <6trfeate2ppvm1mcapgr0g4g2fd3vceab6@4ax.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: host-92-24-222-48.ppp.as43234.net User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 In-Reply-To: <6trfeate2ppvm1mcapgr0g4g2fd3vceab6@4ax.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 46 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1424521415 news.xs4all.nl 2954 [2001:888:2000:d::a6]:43647 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:86026 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.DatabaseError: > raise OSError('database file corrupt or not found.') > else: > try: > with db: > db.execute(sql, parms) > except lite.IntegrityError: > raise ValueError('invalid data') > finally: > db.close() > > Since it's a bit verbose, is there a better way? > > Note: The user of this API has the whole database functionality > abstracted away. Hence the exception channeling in the except clauses. > 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.') -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence