Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Database
  • Python MySQL
  • Python SQLite
  • Python MongoDB
  • PostgreSQL
  • SQLAlchemy
  • Django
  • Flask
  • SQL
  • ReactJS
  • Vue.js
  • AngularJS
  • API
  • REST API
  • Express.js
  • NodeJS
Open In App
Next Article:
Extract Data from Database using MySQL-Connector and XAMPP in Python
Next article icon

Extract Data from Database using MySQL-Connector and XAMPP in Python

Last Updated : 01 Nov, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report

Prerequisites: MySQL-Connector, XAMPP Installation

A connector is employed when we have to use MySQL with other programming languages. The work of mysql-connector is to provide access to MySQL Driver to the required language. Thus, it generates a connection between the programming language and the MySQL Server.

Requirements

  • XAMPP: Database / Server to store and display data.
  • MySQL-Connector module: For connecting the database with the python file. Use the below command to install this module.
pip install mysql-connector  
  • Wheel module: A command line tool for working with wheel files. Use the below command to install this module.
pip install wheel  

Step-by-step Approach:

Procedure to create a table in the database:

  • Start your XAMPP web server.
  • Type http://localhost/phpmyadmin/ in your browser.
  • Go to Database create database with name and click on Create.
  • Create a table with in GEEK database and click on Go.
  • Define column names and click on save.
  • Your table is created.
  • Insert data in your database by clicking on SQL tab then select INSERT.
  • The data in your table is:
  • Now you can perform operation IE display data in your web page using python

Procedure for writing Python program:

  • Import mysql connector module in your Python code.
import mysql.connector  
  • Create connection object.

conn_object=mysql.connector.connect(hostname,username,password,database_name)

Here, you will need to pass server name, username, password, and database name)

  • Create a cursor object.
cur_object=conn_object,cursor()  
  • Perform queries on database.
query=DDL/DML etc  cur_obj=execute(query)  
  • Close cursor object.
cur_obj.close()  
  • Close connection object.
conn_obj.close()  

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

Python3
# import required modules import mysql.connector  # create connection object con = mysql.connector.connect(   host="localhost", user="root",   password="", database="GEEK")  # create cursor object cursor = con.cursor()  # assign data query query1 = "desc geeksdemo"  # executing cursor cursor.execute(query1)  # display all records table = cursor.fetchall()  # describe table print('\n Table Description:') for attr in table:     print(attr)  # assign data query query2 = "select * from geeksdemo"  # executing cursor cursor.execute(query2)  # display all records table = cursor.fetchall()  # fetch all columns print('\n Table Data:') for row in table:     print(row[0], end=" ")     print(row[1], end=" ")     print(row[2], end=" ")     print(row[3], end="\n")      # closing cursor connection cursor.close()  # closing connection object con.close() 

Output:

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


Next Article
Extract Data from Database using MySQL-Connector and XAMPP in Python

S

sravankumar_171fa07058
Improve
Article Tags :
  • Python
  • Python-mySQL
Practice Tags :
  • python

Similar Reads

    Connect MySQL database using MySQL-Connector Python
    While working with Python we need to work with databases, they may be of different types like MySQL, SQLite, NoSQL, etc. In this article, we will be looking forward to how to connect MySQL databases using MySQL Connector/Python.MySQL Connector module of Python is used to connect MySQL databases with
    2 min read
    How to store XML data into a MySQL database using Python?
    In this article, we are going to store XML data into the MySQL database using python through XAMPP server. So we are taking student XML data and storing the values into the database. RequirementsXAMPP server: It is a cross-platform web server used to develop and test programs on a local server. It i
    3 min read
    How to visualize data from MySQL database by using Matplotlib in Python ?
    Prerequisites: Matplotlib in Python, MySQL While working with Python we need to work with databases, they may be of different types like MySQL, SQLite, NoSQL, etc. In this article, we will be looking forward to how to connect MySQL databases using MySQL Connector/Python. MySQL Connector module of Py
    5 min read
    Create Database in MariaDB using PyMySQL in Python
    MariaDB is an open source Database Management System and its predecessor to MySQL. The pymysql client can be used to interact with MariaDB similar to that of MySQL using Python. In this article we will look into the process of creating a database using pymysql. To create a database use the below syn
    2 min read
    Create MySQL Database Login Page in Python using Tkinter
    Prerequisites: Python GUI – tkinter, Python MySQL – Select QueryTkinter is one of the Python libraries which contains many functions for the development of graphic user interface pages and windows. Login pages are important for the development of any kind of mobile or web application. This page is m
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences