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


Groups > comp.lang.python > #12688 > unrolled thread

sqlite3 with context manager

Started byTim Arnold <Tim.Arnold@sas.com>
First post2011-09-02 14:43 -0400
Last post2011-09-06 13:18 -0400
Articles 4 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  sqlite3 with context manager Tim Arnold <Tim.Arnold@sas.com> - 2011-09-02 14:43 -0400
    Re: sqlite3 with context manager Ian Kelly <ian.g.kelly@gmail.com> - 2011-09-02 22:38 -0600
    Re: sqlite3 with context manager Carl Banks <pavlovevidence@gmail.com> - 2011-09-03 00:03 -0700
      Re: sqlite3 with context manager Tim Arnold <Tim.Arnold@sas.com> - 2011-09-06 13:18 -0400

#12688 — sqlite3 with context manager

FromTim Arnold <Tim.Arnold@sas.com>
Date2011-09-02 14:43 -0400
Subjectsqlite3 with context manager
Message-ID<j3r859$rnt$1@foggy.unx.sas.com>
Hi,
I'm using the 'with' context manager for a sqlite3 connection:

with sqlite3.connect(my.database,timeout=10) as conn:
             conn.execute('update config_build set datetime=?,result=?
where id=?',
                               (datetime.datetime.now(), success,
self.b['id']))

my question is what happens if the update fails? Shouldn't it throw an
exception?

I ask because apparently something went wrong yesterday and the code
never updated but I never got any warning.  I rebooted the machine and
everything is okay now, but I'd like to understand what happened.

thanks,
--Tim

[toc] | [next] | [standalone]


#12691

FromIan Kelly <ian.g.kelly@gmail.com>
Date2011-09-02 22:38 -0600
Message-ID<mailman.726.1315024734.27778.python-list@python.org>
In reply to#12688
On Fri, Sep 2, 2011 at 12:43 PM, Tim Arnold <Tim.Arnold@sas.com> wrote:
> Hi,
> I'm using the 'with' context manager for a sqlite3 connection:
>
> with sqlite3.connect(my.database,timeout=10) as conn:
>            conn.execute('update config_build set datetime=?,result=?
> where id=?',
>                              (datetime.datetime.now(), success,
> self.b['id']))
>
> my question is what happens if the update fails? Shouldn't it throw an
> exception?

That depends on why it fails.  If you pass in an id that doesn't exist
in the database, it "successfully" updates 0 rows.  I would guess
that's what happened here.  You should check cursor.rowcount after
running the update query to make sure it actually did something.

Cheers,
Ian

[toc] | [prev] | [next] | [standalone]


#12695

FromCarl Banks <pavlovevidence@gmail.com>
Date2011-09-03 00:03 -0700
Message-ID<7dbe35af-9cc7-42a8-9288-465e542b9ea6@glegroupsg2000goo.googlegroups.com>
In reply to#12688
On Friday, September 2, 2011 11:43:53 AM UTC-7, Tim Arnold wrote:
> Hi,
> I'm using the 'with' context manager for a sqlite3 connection:
> 
> with sqlite3.connect(my.database,timeout=10) as conn:
>              conn.execute('update config_build set datetime=?,result=?
> where id=?',
>                                (datetime.datetime.now(), success,
> self.b['id']))
> 
> my question is what happens if the update fails? Shouldn't it throw an
> exception?

If you look at the sqlite3 syntax documentation, you'll see it has a SQL extension that allows you to specify error semantics.  It looks something like this:

UPDATE OR IGNORE
UPDATE OR FAIL
UPDATE OR ROLLBACK

I'm not sure exactly how this interacts with pysqlite3, but using one of these might help it throw exceptions when you want it to.


Carl Banks

[toc] | [prev] | [next] | [standalone]


#12872

FromTim Arnold <Tim.Arnold@sas.com>
Date2011-09-06 13:18 -0400
Message-ID<j45kkf$b7o$1@foggy.unx.sas.com>
In reply to#12695
On 9/3/2011 3:03 AM, Carl Banks wrote:
> On Friday, September 2, 2011 11:43:53 AM UTC-7, Tim Arnold wrote:
>> Hi,
>> I'm using the 'with' context manager for a sqlite3 connection:
>>
>> with sqlite3.connect(my.database,timeout=10) as conn:
>>               conn.execute('update config_build set datetime=?,result=?
>> where id=?',
>>                                 (datetime.datetime.now(), success,
>> self.b['id']))
>>
>> my question is what happens if the update fails? Shouldn't it throw an
>> exception?
>
> If you look at the sqlite3 syntax documentation, you'll see it has a SQL extension that allows you to specify error semantics.  It looks something like this:
>
> UPDATE OR IGNORE
> UPDATE OR FAIL
> UPDATE OR ROLLBACK
>
> I'm not sure exactly how this interacts with pysqlite3, but using one of these might help it throw exceptions when you want it to.
>
>
> Carl Banks

I see now. You can use 'update or fail' if you have the extensions built 
in: http://docs.python.org/library/sqlite3.html#f1

example of use, line 76:
http://projects.developer.nokia.com/TECwidget/browser/data/montreal/updsqlite.py?rev=7ca2ebd301ed1eff0e2c28283470db060b872cd6

For my case, however, I'll follow Ian's advice and check on the rowcount 
after the update.

thanks for the explanation and advice,
--Tim

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web