[Tutor] sqlite3 COMMIT directive

Tim Golden mail at timgolden.me.uk
Tue Oct 1 12:41:03 CEST 2013


On 01/10/2013 11:28, Alan Gauld wrote:
> On 01/10/13 09:25, Tim Golden wrote:
>> On 01/10/2013 09:03, Alan Gauld wrote:
>>> You don't normally need to use COMMIT when programming SQLite from
>>> Python, the database starts and ends transactions automatically from
>>> within the execute() function.
>>
>> Not so, I'm afraid. If you want autocommit, you need to send an
>> isolation_level of None to the .connect function.
> 
> Hmm, I didn't express that as I should but I was meaning within
> a 'with' block (which the OP was using) based on this comment
> in the docs:
> 
> """
> Connection objects can be used as context managers that automatically
> commit or rollback transactions. In the event of an exception, the
> transaction is rolled back; otherwise, the transaction is committed:
> """
> 
> Now I admit I didn't test it but I read that to imply that the with
> would autocommit. Am I mistaken?

You're sort-of correct. What happens is that the database doesn't enter
autocommit mode (you'll still need to specify the right isolation level
on the .connect for that). Rather, the __exit__ method of the
connection-as-context-manager issues the db.commit() call which will
commit anything outstanding.

TJG



More information about the Tutor mailing list