0% found this document useful (0 votes)
367 views

MySQL Using Python Cheat Sheet PDF

This document provides an overview of how to connect to and interact with a MySQL database using Python. It demonstrates how to establish a connection, execute queries to insert, update, delete and fetch data, use prepared statements to prevent SQL injection, and manage transactions to ensure data consistency. Key steps include importing the MySQL connector library, defining connection parameters, using cursors to execute queries, committing transactions, and closing connections.

Uploaded by

Alex
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
367 views

MySQL Using Python Cheat Sheet PDF

This document provides an overview of how to connect to and interact with a MySQL database using Python. It demonstrates how to establish a connection, execute queries to insert, update, delete and fetch data, use prepared statements to prevent SQL injection, and manage transactions to ensure data consistency. Key steps include importing the MySQL connector library, defining connection parameters, using cursors to execute queries, committing transactions, and closing connections.

Uploaded by

Alex
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

MYSQL IN

PYTHON

BY

FARDEEN A KHAN
https://github.com/I-Fardeen
🚀 Getting Started:
Before you start, make sure you have
the MySQL Connector/Python library
installed.

pip install mysql-connector-python

🌐 Establish a connection
import mysql.connector
# Establish a connection
connection= mysql.connector.connect(
host="localhost",
user="username",
password="password",
database="mydb"
)
🔍 Executing Queries
You can execute SQL queries using a
cursor. Please note a single cursor
can also be used in entire program.

cursor = connection.cursor()

# Execute SQL query


cursor.execute("SELECT*FROM mytable")

# Fetch results
result = cursor.fetchall()

# Don't forget to commit changes


connection.commit()

# Close the cursor and connection


cursor.close()
connection.close()
➕ Inserting Data
Adding data to a table.

cursor = connection.cursor()

# Insert data
sql = "INSERT INTO mytable (column1,
column2) VALUES (%s, %s)"
values = ("value1", "value2")

cursor.execute(sql, values)

# Commit and close

connection.commit()
cursor.close()
✏️ Updating Data
Modify existing data.

cursor = connection.cursor()

# Update data
sql = "UPDATE mytable SET column1 = %s
WHERE column2 = %s"
values =("new_value","criteria_value")

cursor.execute(sql, values)

# Commit and close

connection.commit()
cursor.close()
❌ Deleting Data
Remove data from a table.

cursor = connection.cursor()

# Delete data
sql = "DELETE FROM mytable WHERE
column = %s"
value = "value_to_delete"

cursor.execute(sql,(value,))

# Commit and close

connection.commit()
cursor.close()
🌐 Fetching Data
Retrieve data from a query.

# Execute SQL query


cursor.execute("SELECT * FROM mytable")

# Fetch one row


row = cursor.fetchone()

# Fetch all rows


rows = cursor.fetchall()

# Close the cursor


cursor.close()
📋 Using Prepared Statements
Prevent SQL injection by using
prepared statements.

cursor = connection.cursor(prepared=True)

# Execute prepared statement


stmt = "INSERT INTO mytable (column1,
column2) VALUES (?, ?)"
data = ("value1", "value2")

cursor.execute(stmt, data)

# Commit and close

connection.commit()
cursor.close()
🔐 Transaction Management
Manage transactions to ensure data
consistency.

connection.start_transaction()

try:

# Your database operations here


connection.commit()

except:
# Undo operations in case of error
connection.rollback()

# Close the connection


connection.close()
SAVE THIS POST

FOLLOW FOR
MORE
CONTENT
FARDEEN AHMAD KHAN

https://linkedin.com/in/meetfardeen
https://github.com/I-Fardeen

Read my Technical Articles on:

https://fardeenk.medium.com

You might also like