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

C:UsersYour NameAppDataLocalProgramsPythonPython36-32Scripts>python -m pip install mysql-connector-python

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

import mysql.connector

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

import mysql.connector myfirstpydb = mysql.connector.connect( host="localhost", user="YourDBusername", password="YourDBpassword" ) print(myfirstpydb)

By now, you will be able to use MySQL statements to query the database.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *