9951 explained code solutions for 126 technologies


python-mysqlHow do I use a SELECT statement in Python to query a MySQL database?


In order to use a SELECT statement in Python to query a MySQL database, the following steps should be taken:

  1. Establish a connection to the MySQL database using the mysql.connector library:
import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  passwd="yourpassword"
)
  1. Create a cursor object by calling the cursor() method of the connection object:
mycursor = mydb.cursor()
  1. Use the execute() method of the cursor object to execute the SELECT statement:
mycursor.execute("SELECT * FROM customers")
  1. Fetch all the records from the SELECT statement using the fetchall() method of the cursor object:
myresult = mycursor.fetchall()

for x in myresult:
  print(x)

Output example

('John', 'Highway 21')
('Amy', 'Mountain 21')
('Hannah', 'Valley 345')
  1. Close the cursor and the connection to the database using the close() method of the cursor and connection objects respectively:
mycursor.close()
mydb.close()

Helpful links

Edit this code on GitHub