autocommit

Overview

This connection value (read/write) determines if commits occur automatically or manually. Default: False (automatic).

See also: MariaDB Connector/Python 1.0

EXAMPLES

Connection Option

You can enable auto-commit for the connection using the autocommit connection option:

  • Enabling auto-commit using the connection option:

    try:
       conn = mariadb.connect(
             user="connpy_test",
             password="passwd",
             host="localhost",
             port=3306,
             autocommit=True)
    
    except Exception as e:
       print(f"Connection Error: {e}")
    
  • Enabling auto-commit on existing connection:

    conn.autocommit = True
    

Manual Transactions

When auto-commit is disabled (default), you can manually manage transactions using the commit() and rollback() connection methods:

import mariadb

conn = mariadb.connect(
    host = 'localhost',
    user = 'root',
    password = 'secret',
    )

print(conn.autocommit) # Outputs False

ok = True
# ... make some DB changes here using a cursor object ...

if ok:
    conn.commit()
else:
    conn.rollback() # a closed connection also does a rollback