[Tutor] sqlite3 COMMIT directive

Alan Gauld alan.gauld at btinternet.com
Tue Oct 1 10:03:11 CEST 2013


On 01/10/13 04:16, Alex Kleider wrote:

> And the problem = \
> """
> If the 'cur.execute("COMMIT;")' statement is left by itself, outside of a
> "try/except" clause, I get an error with the following trace back:
> "
> Traceback (most recent call last):
>    File "./uwomeds68.py", line 119, in <module>
>      go_on.insert()
>    File "./uwomeds68.py", line 64, in insert
>      cur.execute("COMMIT;")
> sqlite3.OperationalError: cannot commit - no transaction is active

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. You only need to manually commit if you 
manually create the transaction using BEGIN...

> Without the COMMIT, there's no error but the data doesn't get added to
> the data base.

That suggests that you are opening the wrong database file or you have a 
permissions problem or there is some other error. But you should not 
need a COMMIT.

BTW. I notice you are using an implicit cursor within the with block.
Now while that may work, my personal preference is for explicit 
variables, I just find it keeps the code clearer to explicitly create 
the cursor rather than have a variable appear in my code with no 
definition. It's OK in short snippets but as the code gets bigger
it can get confusing. But that's just my own preference...

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list