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.
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.
- Press Windows Key+R key
- Enter cmd.exe and press Enter
- The command prompt will be appeared
- Use the above windows command and press Enter
- 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
- Read Excel data using Pandas DataFrame
- Python Connect to SQL Database
- How to install pyodbc window
- PIP Install on Windows
- Installing Python
- Overview of Python
Thanks