Install mysql for Python

Kailash Chandra Behera | Friday, July 03, 2020

Introduction

PyMySQL is a module in Python which provides access to connect with Mysql Server Database. This blog provides guideline to install PyMySQL library in local machine and provides code example to connect mysql.

Install mysql for Python

Getting Started

Installing PyMySQL is not a big task for Python developer. A single line of command installs PyMySQL library into machine. Before installing PyMySQL make sure that Python and pip is installed in your machine.

If you are new in Python, refer my articles to install Python and pip. These two articles are helping to install Python and pip. Pip is a Python Package Manager tool that helps to install Python packages or modules into the machine. Hence before installing any packages or modules pip must have installed in the machine.

The below command is helping to install the PyMySQL module into the machine.

Install MySQL on Windows
  pip install PyMySQL  
Install MySQL on Mac
 sudo -H pip install PyMySQL 

Install mysql for Python

Demonstration

This demonstration is conducted in Microsoft Windows 10 and applicable in Windows only, to install the PyMySQL, follow the below steps.

  1. Press Windows Key+R key
  2. Enter cmd.exe and press Enter
  3. The command prompt will be appeared
  4. Use the above windows command and press Enter
  5. The installation process will be started, if everything going fine then the package or module will be installed successfully.

Python mysql query

The below code example describes how to connect MYSQL and fetch data from MYSQL database Table.

 import mysql.connector  
 mydb = mysql.connector.connect(  
  host="serverpath",  
  user="userName",  
  password="Password",  
  database="databaseName"  
 )  
 mycursor = mydb.cursor()  
 mycursor.execute("SELECT * FROM TableName")  
 myresult = mycursor.fetchall()  
 for x in myresult:  
  print(x)  

Related Articles

  1. Read Excel data using Pandas DataFrame
  2. Python Connect to SQL Database
  3. How to install pyodbc window
  4. PIP Install on Windows
  5. Installing Python
  6. Overview of Python

Thanks