Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #86054

Re: try pattern for database connection with the close method

From Peter Otten <__peter__@web.de>
Subject Re: try pattern for database connection with the close method
Date 2015-02-21 18:02 +0100
Organization None
References <6trfeate2ppvm1mcapgr0g4g2fd3vceab6@4ax.com> <mc9tbf$62d$1@ger.gmane.org> <CALwzidmKzZutwKQ-pddx_6YHPTqoix8Fod-JX=YF38dYBSyThQ@mail.gmail.com> <mca84m$bg0$1@ger.gmane.org> <CALwzidnNrRV1kvz+05N7EBRAkkj2iBTxyLDJK=hCTUBzdPtVeA@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.18975.1424538179.18130.python-list@python.org> (permalink)

Show all headers | View raw


Ian Kelly wrote:

> On Sat, Feb 21, 2015 at 8:27 AM, Peter Otten <__peter__@web.de> wrote:
>> Ian Kelly wrote:
>>
>>> On Sat, Feb 21, 2015 at 5:22 AM, Mark Lawrence <breamoreboy@yahoo.co.uk>
>>> wrote:
>>>> 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.')
>>>
>>> This could result in the OSError being misleadingly raised due to some
>>> DatabaseError raised by the execute rather than the connect.
>>
>> The OP probably wants to catch these DatabaseErrors, too. Also, the
>> chance of a misleading traceback has been greatly reduced with the advent
>> of chained exceptions.
>>
> 
> Yes, but the point is that OSError is probably inappropriate in that case.

Perhaps, but the example I gave in my other post:

>>> with open("data.db", "wb") as f:
...     f.write(os.urandom(1024)) # put random bytes into data.db

>>> db = sqlite3.connect("data.db")
>>> db.execute("create table foo (bar, baz);")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
sqlite3.DatabaseError: file is encrypted or is not a database

matches the "database file corrupt" part of the error message provided by 
the OP.

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

try pattern for database connection with the close method Mario Figueiredo <marfig@gmail.com> - 2015-02-21 03:42 +0100
  Re: try pattern for database connection with the close method Chris Kaynor <ckaynor@zindagigames.com> - 2015-02-20 18:59 -0800
  Re: try pattern for database connection with the close method Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-02-21 12:22 +0000
    Re: try pattern for database connection with the close method Mario Figueiredo <marfig@gmail.com> - 2015-02-22 19:41 +0100
      Re: try pattern for database connection with the close method Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-02-22 19:07 +0000
        Re: try pattern for database connection with the close method Mario Figueiredo <marfig@gmail.com> - 2015-02-23 00:25 +0100
      Re: try pattern for database connection with the close method Skip Montanaro <skip.montanaro@gmail.com> - 2015-02-22 13:15 -0600
        Re: try pattern for database connection with the close method Mario Figueiredo <marfig@gmail.com> - 2015-02-23 00:30 +0100
  Re: try pattern for database connection with the close method Ian Kelly <ian.g.kelly@gmail.com> - 2015-02-21 08:16 -0700
  Re: try pattern for database connection with the close method Peter Otten <__peter__@web.de> - 2015-02-21 16:22 +0100
    Re: try pattern for database connection with the close method Mario Figueiredo <marfig@gmail.com> - 2015-02-23 00:35 +0100
  Re: try pattern for database connection with the close method Peter Otten <__peter__@web.de> - 2015-02-21 16:27 +0100
  Re: try pattern for database connection with the close method Ian Kelly <ian.g.kelly@gmail.com> - 2015-02-21 08:50 -0700
  Re: try pattern for database connection with the close method Peter Otten <__peter__@web.de> - 2015-02-21 18:02 +0100

csiph-web