Tags
python
Asked 3 years ago
12 Aug 2021
Views 297
Evalyn

Evalyn posted

How do I connect MySQL with Python ?

How do I connect MySQL with Python ?
andy

andy
answered Aug 13 '21 00:00

you need to install mysql and mysqlclient , mysql-connector packages to connect MySQL with Python.

lets me show you. how to install needed packages to connect MySQL with Python.

to install mysql and mysqlclient , paste the below command at the terminal and press enter

pip install mysql 


and
to install mysql-connector . paste below command at terminal and press enter


pip install mysql-connector 


now make mysql.py file , you can use any filename instead of mysql.py
put below code

import mysql.connector as mysql


after running the above code if no error is generated then all packages are installed well.

than lets connect to MySQL database from Python

import mysql.connector as connector
 
#user your db details
db = connector.connect(
    host = "localhost",
    user = "root", 
    passwd = "",
    database = "anydatabasename"

)

connect() function is used to connect with MySQL database with given login details
Post Answer