How to connect SQL Database in Python

Connecting MYSQL Database in python:

To create the connection between mysql and python we use the connect() method of mysql.connector.

To create connection we follow 5 steps in which we pass the database details like hostname ,username and password in the method calland then retruns the connection.

The Steps are:

STEP-1 :Download and installing the MYSQL Database

we can download and install the MYSQL database for free using the chrome

STEP-2 : Command prompt

After installing the mysql database we need to open the command prompt and we can navigate our command prompt to PIP

STEP-3 : Installation

Using pip install mysql-connector-python we can install the MYSQL Connector and it helps to communicate with the MYSQL Database.

STEP-4 : Test MYSQL Connector

To check whether the installation is successful or not we need run import mysql.connector in IDE.

STEP-5 : Create Connection

To connect sql with python we need to run below code , in place of user we need to create our username and in place of password we need create a password.

import mysql.connector
database=mysql.connector.connect(host="localhost",user="username",password="password")
print(database)

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top