9951 explained code solutions for 126 technologies


python-mysqlHow can I use a while loop in Python to interact with a MySQL database?


Using a while loop in Python to interact with a MySQL database is a fairly common task. To do this, you will need to use the Python MySQL connector library. The following example code will demonstrate how to use a while loop to interact with a MySQL database:

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="user",
  passwd="passwd",
  database="mydatabase"
)

mycursor = mydb.cursor()

sql = "SELECT * FROM customers"

mycursor.execute(sql)

myresult = mycursor.fetchall()

while myresult != None:
  print(myresult)
  myresult = mycursor.fetchall()

This code will connect to a MySQL database, execute a SQL query to select all customers, and then use a while loop to iterate through the results and print them out.

Code explanation

  1. import mysql.connector - imports the Python MySQL connector library
  2. mydb = mysql.connector.connect(host="localhost", user="user", passwd="passwd", database="mydatabase") - establishes a connection to the MySQL database
  3. mycursor = mydb.cursor() - creates a cursor object to interact with the database
  4. sql = "SELECT * FROM customers" - creates a SQL query to select all customers from the database
  5. mycursor.execute(sql) - executes the SQL query
  6. myresult = mycursor.fetchall() - fetches all the results from the query
  7. while myresult != None: - creates a while loop to iterate through the results
  8. print(myresult) - prints out the results
  9. myresult = mycursor.fetchall() - fetches the next set of results

Helpful links

Edit this code on GitHub