Python MySQL
Python Mysql Get Started is today’s topic, and this article is meant to educate the reader.
The Python programming language can be used for database applications.
There are many databases available, but MySQL is one of the most popular.
MySQL Database
This tutorial requires MySQL to be installed on your computer, so that you can experiment with the code examples in the tutorial.
Follow the steps in our article on how to install MySQL on Windows to install MySQL on your computer.
Here’s how you can install MySQL on Linux, including Ubuntu and other flavours.
Our guide to installing MySQL on Mac OS can be found here.
Install MySQL Driver
To access the MySQL database, Python requires a MySQL driver.
The driver “MySQL Connector” will be used in this tutorial.
The “MySQL Connector” should be installed using PIP.
Your Python environment probably already has PIP installed.
You will need to navigate to the location of PIP and type the following command:
Install the “MySQL Connector”:
Example
The MySQL driver for your Windows system is now ready for download and installation.
Test Python MySQL Connector
Using Python, create a Python page with the following content to test whether the installation was successful:
mysql-test.py:
Example
The “MySQL Connector” is now installed and ready to use if the above code was executed without errors.
Create DB Connection
Connect to the Python MySQL database by creating a connection.
You will need your MySQL database username and password:
mysql-connection.py:
Example
By now, you will be able to use MySQL statements to query the database.