Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)
I am currently working with ActiveState Python 3 on Windows os and I want to connect to my MySQL database. I discovered that mysqldb was the module to use. I can't find mysqldb for Python 3.

Kindly let me know how to connect to MySQL in Python 3?

1 Answer

0 votes
by (108k points)

For connecting mysql in Python, you can use pymysql as it is a complete Python MySQL client.

It works with Python 3.x and doesn't have any dependencies.

import pymysql

conn = pymysql.connect(host='127.0.0.1', unix_socket='/tmp/mysql.sock', user='root', passwd=None, db='mysql')

cur = conn.cursor()

cur.execute("SELECT Host,User FROM user")

for r in cur:

    print(r)

cur.close()

conn.close()

Browse Categories

...