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

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago