sqlite3 with context manager

T

Tim Arnold

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
 
I

Ian Kelly

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
 
C

Carl Banks

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
 
T

Tim Arnold

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...?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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
VetaMcRae
Top