This tutorial will teach you what MySQL is, what the prerequisites are for using MySQL in Python, and where you can download and install MySQL and the MySQL Python Connector.
What is MySQL?
MySQL is an open-source relational database management system (RDBMS) that is based on Structured Query Language (SQL). It is used to store data and is not a programming language. SQL is used to program a MySQL database. MySQL can run on any operating system and is a popular database. It can be downloaded and installed for free from the official website. Thus,
- SQL can thus be used to program a MySQL database.
- MySQL’s main advantage is that it can run on any operating system.
What is MySQL Connector?
Python MySQL Connector is a Python driver that allows Python and MySQL to work together. This Python MySQL library supports data conversion between Python and MySQL. MySQL Connector API is written entirely in Python and does not rely on any third-party libraries.
The MySQL Connector can be obtained in two ways:
The first option is to download and install it from the following URL: https://dev.mysql.com/downloads/connector/python/.
Another option is to use PIP to install the “MySQL Connector,” which is already present in your Python environment. So you can download and install simply by navigating your command line to the PIP location and typing:
pip install mysql-connector-python
Test the MySQL Connector:
To check if the MySQL connector is installed correctly or not in your System you just need to write the following code:
If the above code runs successfully, without raising any error then it means installation is done correctly.
Creating the Connection:
We can connect to the MySQL server using the connect() method.
# importing required libraries import mysql.connector dataBase = mysql.connector.connect( host ="localhost", user ="user", passwd ="password" ) print(dataBase) # Disconnecting from the server dataBase.close()
<mysql.connector.connection_cext.CMySQLConnection object at 0x7f73f0191d00>
The output above indicates that you have successfully connected to the MySQL database. In the following tutorial, we will begin querying the database using SQL statements.
Note: also read about Python Logging Classes and Functions
Please follow me to read my latest post on programming and technology if you like my post.
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Leave a Reply