View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
While working with Python we need to work with databases, they may be of different types like MySQL, SQLite, NoSQL, etc. In this article, we will be
looking forward to how to connect MySQL databases using MySQL Connector/Python.
MySQL Connector module of Python is used to connect MySQL databases with the Python programs, it does that using the Python Database API Specification v2.0 (PEP 249). It uses the Python standard library and has no dependencies.
Connecting to the Database
In the following example we will be connecting to MySQL database using connect()
Example:
Python3
import mysql.connector
conn = mysql.connector.connect(user = 'username',
host = 'localhost',
database = 'database_name')
print(conn)
conn.close()
Output:
Also for the same, we can use
connection.MySQLConnection() class instead of connect():
Example:
Python3
from mysql.connector import connection
conn = connection.MySQLConnection(user = 'username',
host = 'localhost',
database = 'database_name')
print(conn)
conn.close()
Output:
Another way is to pass the dictionary in the connect() function using ‘**’ operator:
Example:
Python3
from mysql.connector import connection
dict = {
'user': 'root',
'host': 'localhost',
'database': 'College'
}
conn = connection.MySQLConnection(**dict)
print(conn)
conn.close()
Output:
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Prerequisite: Python Language
Introduction
MySQL is a Relational Database Management System (RDBMS) whereas the structured Query Language (SQL) is the language used for handling the RDBMS using commands i.e Creating, Inserting, Updating and Deleting the data from the databases.
A connector is employed when we have to use MySQL with other programming languages. The work of mysql-connector is to provide access to MySQL Driver to the required language. Thus, it generates a connection between the programming
language and the MySQL Server.
Installation:
To install Python-mysql-connector module, one must have Python and PIP, preinstalled on their system. To check if your system already contains Python, go through the following instructions:
Open the Command line(search for cmd in the Run dialog( + R).
Now run the following command:
If Python is already installed, it will generate a message with the Python version
available.
If Python is not present, go through How to install Python on Windows and Linux? and follow the instructions
provided.
PIP is a package management system used to install and manage software packages/libraries written in Python. These files are stored in a large “on-line repository” termed as Python Package Index (PyPI).
To check if PIP is already installed on your system, just go to the command line and execute the following command:
If PIP is not present, go through How to install PIP on Windows and Linux?
Windows
mysql-connector method can be installed on Windows with the use of following command:
pip install mysql-connector-pythonLinux
mysql-connector method can be installed on Linux with the use of following command:
pip3 install mysql-connector