This is an introductory tutorial on Python-MySQL programming. You will learn why MySQL is one of the best options as a database for Python programs and what is the step by step procedure to download, install and connect the python program with the MySQL database.
Why MySQL With Python?
MySQL is, without a doubt, one of the most popular and widely used database management system in the market today. It is also an open-source SQL database which is capturing a good market share day by day. Major tech companies such as amazon, google, LinkedIn, and Uber uses MySQL for their operations.
Python provides all the tools required to connect with the MySQL database without any hassle and messy steps. Also, python is one of the easiest programming languages to learn and provides easy syntax to do operations with the MySQL database.
Python provides a package manager called “pip” which helps us to install and manage the packages required in the program. Using the pip, we can easily install any package and import it into the program without setting up other things such as environment variables and global installation.
Prerequisites for Python-MySQL Programming
Note that, the Python-MySQL series will not teach you syntaxes of SQL/MySQL query language. You must be familiar with the MySQL database and basic operations such as insert, update, select and delete.
Also, you should have basic knowledge of python programming such as loops, functions, exception handling and using the pip package manager.
What You Will learn
At the end of this series, you will learn to –
- Connect MySQL with the Python program using the python-MySQL connector.
- Insert the data into the MySQL table.
- Update the data of the MySQL table.
- Fetch the data from the MySQL table.
- Delete the data from the MySQL table.
- Exception handling while querying.
- Some good practices that are required when writing python code.
How to Install Python
The first thing you will require is the python on your machine. Python must be installed globally on your machine and the path should be set in the environment variables.
If Python is not installed on your machine, download the python from the official website and install it on your machine. While installation, follow the steps shown on the prompt.
How to Install MySQL
If the MySQL server is not installed on your machine, we recommend you to follow these instructions on the official MySQL website in order to download, install and use it.
If you are a Windows user, it is recommended to download the MySQL Installer which will take care of the entire process to install required programs.
How to Connect Python with MySQL
In order to connect a python program to the MySQL database, we will need a database driver. A database driver is a small software that allows your application to connect and interact with a database system.
Python requires a driver to communicate with the database vendor. These drivers are third-party modules and can be downloaded and installed using the package manager.
In python, we have many database drivers available to interact with the MySQL database. However, MySQL connector/python is the most popular among them.
Install Python-MySQL Database Driver
To download the MySQL connector, we can use the pip package manager.
PIP install mysql-connector-pythonCode language: Bash (bash)
Note that, the pip package manager must be installed on your machine and the environment variable be set already.
To test the installation of the MySQL connector, open the Python terminal and enter the following command-
import mysql.connectorCode language: Python (python)
If the above command executes and shows no errors, means the connector is installed successfully.
In the next tutorial, we will see how to import a MySQL connector in a python program and connect it with the MySQL database.
If you are done with all software installations, you can head towards the next tutorial to learn how to connect a python program to the MySQL database in easy steps.