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.
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.
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.
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:
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.
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
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.