Back to Python
2026-01-316 min read

MySQL Comments (Python Programming)

Learn MySQL Comments (Python Programming) step by step with clear examples and exercises.

Title: MySQL Comments (Python Programming)

Why This Matters

In Python programming, comments are essential for improving code readability and documentation. When working with databases like MySQL, it's crucial to understand how to use comments effectively to manage database structures, optimize queries, and document your work. This lesson will guide you through the process of using MySQL comments in Python.

Comments play a vital role in making code more readable, maintainable, and easier for others (or future you) to understand. They also serve as documentation, making it simpler to collaborate on projects and maintain the codebase over time. In this lesson, we will explore various types of comments and their usage in Python and MySQL.

Prerequisites

Before diving into MySQL comments, ensure you have a good understanding of:

  1. Basic Python syntax and data types
  2. SQL (Structured Query Language) fundamentals
  3. Connecting to MySQL databases using Python libraries such as mysql-connector-python
  4. Familiarity with database design principles and common database operations like creating, reading, updating, and deleting (CRUD)
  5. Understanding how to execute SQL queries within a Python script

Core Concept

Single-line Comments

In Python, single-line comments are created by adding a # symbol before the comment text:

This is a single-line comment in Python


When your Python script connects to a MySQL database, you can use the same syntax for single-line comments within SQL queries:

import mysql.connector

mydb = mysql.connector.connect(

host="localhost",

user="yourusername",

password="yourpassword",

database="mydatabase"

)

mycursor = mydb.cursor()

Create a table in MySQL

mycursor.execute("CREATE TABLE employees (id INT, firstname VARCHAR(255), lastname VARCHAR(255))")


### Multi-line Comments
For multi-line comments in Python, you can use triple quotes (`"""` or `'''') to create a block of text:

"""

This is a multi-line comment in Python.

You can include multiple lines of documentation here.

"""


In SQL queries, triple quotes are not supported; instead, you can use the `--` syntax for multi-line comments:

import mysql.connector

mydb = mysql.connector.connect(

host="localhost",

user="yourusername",

password="yourpassword",

database="mydatabase"

)

mycursor = mydb.cursor()

Create a multi-line comment in MySQL

mycursor.execute("""

CREATE TABLE employees (

id INT,

firstname VARCHAR(255),

lastname VARCHAR(255)

-- This is a multi-line comment in MySQL

)

""")


### Documenting SQL Queries with Python Docstrings
Python docstrings (triple quotes) can also be used to document your SQL queries:

import mysql.connector

mydb = mysql.connector.connect(

host="localhost",

user="yourusername",

password="yourpassword",

database="mydatabase"

)

mycursor = mydb.cursor()

"""

This function creates a table in the MySQL database called employees with columns id, firstname, and lastname.

Parameters:

None

Returns:

None

"""

def create_employees_table():

mycursor.execute("""

CREATE TABLE employees (

id INT,

firstname VARCHAR(255),

lastname VARCHAR(255)

)

""")

create_employees_table()

Worked Example

Let's create a simple Python script that connects to a MySQL database, creates a table, and documents the process using comments:

import mysql.connector

Connect to the MySQL database

mydb = mysql.connector.connect(

host="localhost",

user="yourusername",

password="yourpassword",

database="mydatabase"

)

Create a cursor object

mycursor = mydb.cursor()

Document our SQL query to create a table called employees

"""

This function creates a table in the MySQL database called employees with columns id, firstname, and lastname.

Parameters:

None

Returns:

None

"""

def create_employees_table():

mycursor.execute("""

CREATE TABLE employees (

id INT,

firstname VARCHAR(255),

lastname VARCHAR(255)

)

""")

Execute the function to create the table

create_employees_table()

Commit the changes and close the connection

mydb.commit()

mydb.close()

Common Mistakes

  1. Forgetting to use comments: Proper documentation is essential for understanding your code, especially when working with complex database structures.
  2. Overusing comments: While comments are useful, too many can make the code difficult to read and maintain. Strive for a balance between clarity and conciseness.
  3. Mixing Python and SQL comment syntax: Ensure you're using the correct comment syntax for each language (# for Python and -- or triple quotes for MySQL).
  4. Not documenting complex queries: Documenting complex SQL queries can help others understand your intentions, making it easier to collaborate on projects.
  5. Ignoring docstrings: Docstrings are a powerful tool for documenting functions and classes in Python. Make use of them to provide clear documentation for your database operations.
  6. Not using comments to explain complex database structures: Complex database structures can be difficult to understand without proper annotation, so it's essential to use comments to explain relationships between tables, indexes, and other elements.
  7. Not updating comments when code changes: As your code evolves, ensure that your comments are updated to reflect the current state of the codebase.

Practice Questions

  1. Write a Python script that creates a table called customers with columns customer_id, firstname, lastname, and email. Document the SQL query using a docstring.
import mysql.connector

def create_customers_table():
"""
This function creates a table in the MySQL database called customers with columns customer_id, firstname, lastname, and email.

Parameters:
None

Returns:
None
"""
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
mycursor.execute("""
CREATE TABLE customers (
customer_id INT,
firstname VARCHAR(255),
lastname VARCHAR(255),
email VARCHAR(255)
)
""")
mydb.commit()
mydb.close()
  1. Write a multi-line comment in MySQL explaining the purpose of the customers table you created in question 1.
-- The customers table stores information about each customer, including their unique identifier, first name, last name, and email address.
  1. Write a Python function that inserts a new customer into the customers table with the following data: customer_id=1, firstname='John', and lastname='Doe'. Document the function using a docstring.
import mysql.connector

def insert_new_customer(customer_id, firstname, lastname):
"""
This function inserts a new customer into the customers table with the given customer_id, firstname, and lastname.

Parameters:
customer_id (int): The unique identifier for the customer.
firstname (str): The first name of the customer.
lastname (str): The last name of the customer.

Returns:
None
"""
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
insert_query = "INSERT INTO customers (customer_id, firstname, lastname) VALUES (%s, %s, %s)"
values = (customer_id, firstname, lastname)
mycursor.execute(insert_query, values)
mydb.commit()
mydb.close()
  1. Write a SQL query to update the email of the customer with customer_id=1 to john.doe@example.com. Document the query using a multi-line comment in MySQL.
-- Update the email for customer with id 1 to john.doe@example.com
UPDATE customers SET email = 'john.doe@example.com' WHERE customer_id = 1;

FAQ

  1. Why should I use comments in my Python code?
  • Comments improve readability and make it easier for others (or future you) to understand your code. They also serve as documentation, making it simpler to collaborate and maintain the project.
  1. Can I use triple quotes for single-line comments in MySQL?
  • No, triple quotes are not supported for single-line comments in MySQL. Instead, use the -- syntax or multi-line comments with triple quotes.
  1. What is a docstring, and why should I use them in my Python code?
  • A docstring is a block of text that provides documentation for functions, classes, and modules in Python. Docstrings help others understand your code, making it easier to collaborate and maintain the project.
  1. How can I document complex SQL queries in Python?
  • You can use Python docstrings (triple quotes) to document your SQL queries. This helps others understand your intentions when working with databases like MySQL.
  1. What are best practices for using comments in my code?
  • Use comments to explain complex sections of your code, clarify the purpose of variables and functions, and provide context for database structures. Avoid overusing comments, as too many can make the code difficult to read and maintain.
MySQL Comments (Python Programming) | Python | XQA Learn