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

Chris Angelico rosuav at gmail.com
Wed May 31 10:18:50 EDT 2017


On Thu, Jun 1, 2017 at 12:10 AM, Jon Ribbens <jon+usenet at unequivocal.eu> wrote:
> On 2017-05-31, DFS <nospam at dfs.com> wrote:
>> On 5/31/2017 6:26 AM, Jon Ribbens wrote:
>>> Yes, this is indeed a problem with DB-API - you have to keep *two*
>>> objects around all the time (the connection and the cursor) and pass
>>> them to functions, etc, when in any sensible system as used by all
>>> other languages you would only need to pass the connection.
>>
>> You only need to pass the cursor.
>
> ... unless you want to call .commit() or .rollback().
> Which is where we came in.

Technically you CAN commit from the cursor:

cur.connection.commit()

Proof:

>>> import psycopg2
>>> conn = psycopg2.connect("")
>>> cur = conn.cursor()
>>> cur.connection is conn
True

ChrisA



More information about the Python-list mailing list