Select From a Table
To select from a table in MySQL, use the “SELECT” statement:
Example
Select all records from the “customers” table, and display the result:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM customers")
myresult = mycursor.fetchall()
for x in myresult:
print(x)
Note: We use the fetchall()
method, which fetches all rows from the last executed statement.