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.
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,
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
To check if the MySQL connector is installed correctly or not in your System you just need to write the following code:
import mysql.connector
If the above code runs successfully, without raising any error then it means installation is done correctly.
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()
Output:
<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.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
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.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…