sqlite3 with context manager

Ian Kelly ian.g.kelly at gmail.com
Sat Sep 3 00:38:22 EDT 2011


On Fri, Sep 2, 2011 at 12:43 PM, Tim Arnold <Tim.Arnold at 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



More information about the Python-list mailing list