Back

Explore Courses Blog Tutorials Interview Questions
+3 votes
5 views
in Python by (4k points)
I want to use python to connect to a MySQL database, how can I do that?

2 Answers

0 votes
by (46k points)

Just download a Python library named "connector/python" by Oracle from here and there're no binaries to install.

Cheers......!!

0 votes
by (106k points)
edited by
  • For connecting Python to MySQL database these are the following steps you need to follow:-

  • The first thing is you need to install MySQL driver because Python needs this to access its database. For that you need to open your command prompt and write the following command:

python -m pip install mysql-connector

  • After completing the above-mentioned step you would have downloaded and installed  MySQL in your system.

  • To check whether the installation is successful or not you need to test MySQL connector by using the following piece of code.

import mysql.connector

  • Once this code runs successfully it means installation is done and ready to use.
  • Now start creating connections to the database by using the username and password of the MySQL database that you would have set at the time of installation. Following is the code you can use:-

import mysql.connector

mydb = mysql.connector.connect(

host="localhost",

user="yourusername",

passwd="yourpassword"

)

print(mydb)

You can use the following video tutorials to clear all your doubts:-

If you are looking for upskilling yourself in python you can join our Python Training and learn from the industry expert.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Apr 6, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer
asked Aug 20, 2019 in Java by Krishna (2.6k points)
0 votes
0 answers
asked Feb 17, 2021 in Java by Harsh (1.5k points)

Browse Categories

...