Connecting to MySQL Server

We can connect to the MySQL server using the connect() method. 

Python3




# 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>

Note: For more information, refer to Connect MySQL database using MySQL-Connector Python.

Python MySQL

Python MySQL Connector is a Python driver that helps to integrate Python and MySQL. This Python MySQL library allows the conversion between Python and MySQL data types. MySQL Connector API is implemented using pure Python and does not require any third-party library. 

This Python MySQL tutorial will help to learn how to use MySQL with Python from basics to advance, including all necessary functions and queries explained in detail with the help of good Python MySQL examples. So, let’s get started.

Similar Reads

Installation

To install the Python-mysql-connector module, one must have Python and PIP, preinstalled on their system. If Python and pip are already installed type the below command in the terminal....

Connecting to MySQL Server

We can connect to the MySQL server using the connect() method....

Creating Database

...

Creating Tables

After connecting to the MySQL server let’s see how to create a MySQL database using Python. For this, we will first create a cursor() object and will then pass the SQL command as a string to the execute() method. The SQL command to create a database is –...

Insert Data into Tables

...

Fetching Data

For creating tables we will follow the similar approach of writing the SQL commands as strings and then passing it to the execute() method of the cursor object. SQL command for creating a table is –...

Update Data

...

Delete Data from Table

To insert data into the MySQL table Insert into query is used....

Drop Tables

...

Python MySQL Exercises

...

Python MySQL Applications and Projects

We can use the select query on the MySQL tables in the following ways –...