Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #86145
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Subject | Re: try pattern for database connection with the close method |
| Date | 2015-02-22 19:07 +0000 |
| References | <6trfeate2ppvm1mcapgr0g4g2fd3vceab6@4ax.com> <mailman.18955.1424521415.18130.python-list@python.org> <mk8keahatht34gt0kq1ncmtaskdu2s3arc@4ax.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.19021.1424632052.18130.python-list@python.org> (permalink) |
On 22/02/2015 18:41, Mario Figueiredo wrote:
> On Sat, 21 Feb 2015 12:22:58 +0000, Mark Lawrence
> <breamoreboy@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.
>
Where in the documentation does it state that? If it does, it certainly
breaks my expectations, as I understood the whole point of Python
context managers is to do the tidying up for you. Or have you misread
what it says here
https://docs.python.org/3/library/sqlite3.html#using-the-connection-as-a-context-manager
?
>>> import sqlite3
>>> with
sqlite3.connect(r'C:\Users\Mark\Documents\Cash\Data\cash.sqlite') as db:
... db.execute('select count(*) from accounts')
...
<sqlite3.Cursor object at 0x00000000032C70A0>
>>> db.close()
>>>
Looks like you're correct. Knock me down with a feather, Clevor Trevor.
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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