Topics

Python MySQL Introduction

Welcome to a tutorial on MySQL with Python. Here you will learn about MySQL.

To start, we will discuss the prerequisites needed to learn MySQL.

 

Prerequisites for Python MySQL

  • Python Variable, Python Data types, Control Structures, Loops, and many more.
  • Basics of SQL. 

 

What is MySQL?

MySQL is known to be an open-source, relational database management system(RDBMS) that is based on Structured Query Language (SQL). Most importantly, MySQL is used to store data, but it is not used in creating programs. Hence, MySQL is not a programming language. Use this link to read more.

Key points:

  • SQL is used to program a MySQL Database.
  • One of the important advantages of MySQL is that it can run on any of the Operating systems.
  • And, MySQL is one of the Popular databases.

In the next tutorial, you be introduced to an example, so you have to install MySQL on your PC. You can visit this website to download and in install the MySQL database: https://www.mysql.com/downloads/

When you are done installing the MySQL, then you install the MySQL connector for the python. Let's see what a MySQL connector is all about.

 

MySQL Connector

Python typically needs a MySQL Driver which is used to access the MySQL Database and the "MySQL Connector" is the Driver needed. This MySQL Connector enables the Python programs to access the MySQL database.

There are two possible ways to download the MySQL Connector:

  • The first method is to follow the link underlined here to download and install it. https://dev.mysql.com/downloads/connector/python/
  • The second way is by making use of PIP to install the "MySQL Connector" because PIP is already installed in your Python environment. Thus, you just have to download and install just by navigating your command line to the location of PIP and By writing:
python -m pip install mysql-connector-python

But, I will recommend that you make use of the PIP method.

After you have installed the MySQL Driver which is the "MySQL Connector". However, you can confirm by testing the MySQL Connector.

 

Test the MySQL Connector

This can be done by just typing the code below:

import mysql.connector

Thus, if the code written above, runs successfully without any error response, then your installation was successful. 

So, we will create the connection with the help of the connector

 

Creating the Connection

This is the first step to Text MySQL Connector, as we will connect to the database using the username and the password of MySQL. In the case where you forgot your username or password, create a new user with a password. Check the example below:

###Connecting to the database

###Let us import mysql.connector

import mysql.connector

## Now connecting to the database using 'connect()' method
## it takes 3 required parameters 'host', 'user', 'passwd'

mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  password="yourpassword"
)

print(mydb)

Output:

<mysql.connector.connection_cext.CMySQLConnection object at 0x0000023F50726518>

The output of our example shows that you have connected to the MySQL Database.