import mysql.connector
# Establish a connection to the database
db = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="yourdatabase"
)
# Create a cursor object to execute queries
cursor = db.cursor()
# Define the SQL query with the ORDER BY clause
query = "SELECT * FROM tablename ORDER BY columnname ASC"
# Execute the query
cursor.execute(query)
# Fetch all rows returned by the query
rows = cursor.fetchall()
# Display the rows
for row in rows:
print(row)
# Close the database connection
db.close()