9951 explained code solutions for 126 technologies


python-mysqlHow can I use Python to manage MySQL transactions?


Python can be used to manage MySQL transactions using the MySQL Connector/Python library. This library provides an interface for connecting to a MySQL database server and performing various operations. The following example code shows how to create a connection to a MySQL server and execute a transaction:

import mysql.connector

# Create a connection to the MySQL server
conn = mysql.connector.connect(
    host="localhost",
    user="user",
    passwd="password",
    database="database"
)

# Create a cursor to execute queries
cursor = conn.cursor()

# Start a transaction
cursor.execute("START TRANSACTION")

# Execute a query
cursor.execute("SELECT * FROM table")

# Commit the transaction
cursor.execute("COMMIT")

# Close the connection
conn.close()

The code above will connect to a MySQL server, create a cursor object, start a transaction, execute a query, commit the transaction, and close the connection.

Code explanation

  1. import mysql.connector - Imports the MySQL Connector/Python library.
  2. conn = mysql.connector.connect() - Creates a connection to the MySQL server.
  3. cursor = conn.cursor() - Creates a cursor object to execute queries.
  4. cursor.execute("START TRANSACTION") - Starts a transaction.
  5. cursor.execute("SELECT * FROM table") - Executes a query.
  6. cursor.execute("COMMIT") - Commits the transaction.
  7. conn.close() - Closes the connection.

For more information on using Python to manage MySQL transactions, see the MySQL Connector/Python documentation.

Edit this code on GitHub