Categories: python

MySQL with Python

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:

import mysql.connector

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()

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

Follow Me

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

Recent Posts

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

1 day ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

3 weeks ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

1 month ago

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

1 month ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

2 months ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

2 months ago