Search code examples
sqlpython-3.xsql-server-2016pyodbc

If I have autocommit=True for pyodbc connection, can I assume I don't have to do cursor commits after an update or insert?


I have the example code below. Do I still need a cursor commit after the last line? I am running against a SQL server 2016 DB host if that matters.

import pyodbc
cnxn = pyodbc.connect('Driver=SQL Server;Server=localhost;Database=mydb;Trusted_Connection=yes;')
cnxn.autocommit = True
cursor = cnxn.cursor()
cursor.execute('update mytable set myfield=1')

Solution

  • No, you don't need to do the cursor.commit()

    cnxn.autocommit = True
    

    This line in your code will commit all your transactions to the SQL