The Wayback Machine - https://web.archive.org/web/20261010143105/https://www.geeksforgeeks.org/python/extract-data-from-database-using-mysql-connector-and-xampp-in-python/

Extract Data from a MySQL Database Using Python and XAMPP

Last Updated : 7 Oct, 2026

MySQL Connector allows applications to communicate with MySQL databases. After establishing a connection, SQL queries can be executed to retrieve and process database records. This example demonstrates how to connect to a MySQL database running on XAMPP, execute SELECT queries, and fetch the returned data using Python.

Prerequisites

  • XAMPP
  • MySQL Connector/Python
  • phpMyAdmin (included with XAMPP)

Install MySQL Connector/Python:

pip install mysql-connector-python

Create the Database

Create a MySQL database and insert some sample data using XAMPP and phpMyAdmin.

Step 1: Launch the XAMPP Control Panel and start the Apache and MySQL services.

Step 2: Open phpMyAdmin and click New, enter GEEK as the database name, and click Create:

Create a table with in GEEK database and click on Go:

Define column names and click on save:

Table is created:

Insert data in database by clicking on SQL tab then select INSERT:

The data in table is:

Python Implementation

Once the database and table are ready, connect to the MySQL database using the mysql.connector module and execute SQL queries to retrieve the stored records.

Step 1: Import the Required Module

import mysql.connector

Step 2: Use mysql.connector.connect() to establish a connection by providing the host, username, password, and database name.

connection = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="GEEK"
)

Step 3: Create a cursor object to execute SQL queries.

cursor = connection.cursor()

Step 4: Execute the required SQL query using the execute() method.

query = "SELECT * FROM geeksdemo"
cursor.execute(query)

Step 5: Retrieve all matching records using fetchall() and display them.

records = cursor.fetchall()

for record in records:
print(record)

Step 6: Close the cursor and database connection after completing the database operations.

cursor.close()
connection.close()

Below is the complete Python program based on the above approach:

Python
import mysql.connector
from mysql.connector import Error

connection = None
cursor = None

try:
    # Create a connection to the MySQL database
    connection = mysql.connector.connect(
        host="localhost",
        user="root",
        password="",
        database="GEEK"
    )

    if connection.is_connected():
        print("Connected to the MySQL database.\n")

        # Create a cursor object
        cursor = connection.cursor()

        # Display table structure
        describe_query = "DESCRIBE geeksdemo"
        cursor.execute(describe_query)

        print("Table Description:")
        for column in cursor.fetchall():
            print(column)

        # Retrieve all records
        select_query = "SELECT * FROM geeksdemo"
        cursor.execute(select_query)

        records = cursor.fetchall()

        print("\nTable Data:")
        for record in records:
            print(record)

except Error as err:
    print(f"Error: {err}")

finally:
    # Close the cursor
    if cursor is not None:
        cursor.close()

    # Close the database connection
    if connection is not None and connection.is_connected():
        connection.close()
        print("\nMySQL connection closed.")

Output:

Explanation:

  • mysql.connector is imported to connect the Python program to the MySQL database.
  • mysql.connector.connect() establishes a connection using the specified host, username, password, and database name.
  • connection.cursor() creates a cursor object to execute SQL queries.
  • The DESCRIBE query retrieves the structure of the geeksdemo table, while the SELECT query fetches all records from the table.
  • fetchall() returns all query results, which are displayed using a for loop.
  • try-except handles database-related errors, and the finally block closes the cursor and database connection to release resources.

Note: XAMPP Apache and MySQL should be kept on during the whole process. 

Comment