Python DB API - commit() v. execute("commit transaction")?

Skip Montanaro skip.montanaro at gmail.com
Fri Jun 2 14:55:49 EDT 2017


On Fri, Jun 2, 2017 at 11:14 AM, Dennis Lee Bieber <wlfraed at ix.netcom.com>
wrote:

> My conclusion:
>         If using a DB-API compliant adapter, explicitly issuing "begin" and
> "commit" via .execute() should be avoided if one expects to be portable
> (change the adapter from one DBMS to another).
>         Learn the behavior of the adapter (does any SQL start a
> transaction, or
> only INSERT/UPDATE/DELETE/REPLACE -- the latter seems to be the current
> SQLite3 documented behavior, exclusive of both editions of the "Definitive
> Guide" which imply that an active transaction will be commited upon
> executing a SELECT [Python help file for module states that SELECT does
> /not/ commit]) so you understand when it should be IN or OUT of a
> transaction state. *
>

I just checked, and the sqlite3 adapter I have access to (Python 2.7.13 in
a Conda env, module version 2.6.0, SQLite3 3.13.0) has no autocommit
attribute at all. I checked at the module, connection and cursor levels.
I'm using pyodbc via another layer added by others at work to connect to
SQL Server. That extra layer explicitly sets autocommit to True on the
underlying Connection object before returning it to the caller.

In my case, my code isn't terribly large. I think it's safer to set
autocommit to False and be explicit in my use of transactions.

Skip



More information about the Python-list mailing list