Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)
I want to connect to the MySQL database in python, how to do it? Kindly guide me!

1 Answer

0 votes
by (108k points)

There is one package named mysql connector which works for pure Python, and it is system independent, and dead simple to install. You just download, double-click, confirm the license agreement, and go. There is no requirement for Xcode, MacPorts, compiling, restarting …

Then you connect like:

import mysql.connector    

cnx = mysql.connector.connect(user='scott', password='tiger',

                              host='127.0.0.1',

                              database='employees')

try:

   cursor = cnx.cursor()

   cursor.execute("""

      select 3 from your_table

   """)

   result = cursor.fetchall()

   print result

finally:

    cnx.close()

Related questions

+3 votes
2 answers
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

...