9951 explained code solutions for 126 technologies


python-mysqlHow can I use Python to create MySQL queries?


Creating MySQL queries with Python is possible with the help of the MySQL Connector/Python library. This library provides an interface for connecting to a MySQL database server from Python and running SQL statements.

To use the library, you must first install it. This can be done with the pip package manager:

pip install mysql-connector-python

Once the library is installed, you can connect to a MySQL database server and execute queries using the following code:

import mysql.connector

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

mycursor = mydb.cursor()

mycursor.execute("SELECT * FROM customers")

myresult = mycursor.fetchall()

for x in myresult:
  print(x)

The code above connects to a MySQL database server, then creates a cursor object and executes an SQL statement to select all records from the customers table. The results of the query are stored in the myresult variable, which can then be looped over to print out the results.

Code explanation

  1. import mysql.connector - Imports the MySQL Connector/Python library.
  2. mydb = mysql.connector.connect(...) - Connects to the MySQL database server.
  3. mycursor = mydb.cursor() - Creates a cursor object.
  4. mycursor.execute("SELECT * FROM customers") - Executes an SQL statement to select all records from the customers table.
  5. myresult = mycursor.fetchall() - Fetches all the results of the query and stores them in the myresult variable.
  6. for x in myresult: print(x) - Loops over the results of the query and prints them out.

For more information on using the MySQL Connector/Python library, see the official documentation.

Edit this code on GitHub