Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
How to Install MySQLdb module for Python in Linux?
Next article icon

While working with Python we need to work with databases, they may be of different types like MySQL, SQLite, NoSQL, etc. In this article, we will be looking forward to how to connect MySQL databases using MySQL Connector/Python.
MySQL Connector module of Python is used to connect MySQL databases with the Python programs, it does that using the Python Database API Specification v2.0 (PEP 249). It uses the Python standard library and has no dependencies.
 

Connecting to the Database

The mysql.connector provides the connect() method used to create a connection between the MySQL database and the Python application. The syntax is given below.

Syntax:

Conn_obj= mysql.connector.connect(host = <hostname>, user = <username>, passwd = <password>)

Theconnect() function accepts the following arguments.

Hostname - It represents the server name or IP address on which MySQL is running.
Username - It represents the name of the user that we use to work with the MySQL server. By default, the username for the MySQL database is root.
Password - The password is provided at the time of installing the MySQL database. We don't need to pass a password if we are using the root.
Database - It specifies the database name which we want to connect. This argument is used when we have multiple databases.
 

In the following example we will be connecting to MySQL database using connect()
Example:
 

Python3
# Python program to connect# to mysql databaseimportmysql.connector# Connecting from the serverconn=mysql.connector.connect(user='username',host='localhost',database='database_name')print(conn)# Disconnecting from the serverconn.close()

Output:
 

python-mysql-connect-1


Also for the same, we can use connection.MySQLConnection() class instead of connect():
Example:
 

Python3
# Python program to connect# to mysql databasefrommysql.connectorimportconnection# Connecting to the serverconn=connection.MySQLConnection(user='username',host='localhost',database='database_name')print(conn)# Disconnecting from the serverconn.close()

Output:
 

python-mysql-connect-2


Another way is to pass the dictionary in the connect() function using '**' operator:
Example:
 

Python3
# Python program to connect# to mysql databasefrommysql.connectorimportconnectiondict={'user':'root','host':'localhost','database':'College'}# Connecting to the serverconn=connection.MySQLConnection(**dict)print(conn)# Disconnecting from the serverconn.close()

Output:
 

python-mysql-connect-3


 Example

Python3
# importing required librariesimportmysql.connectordataBase=mysql.connector.connect(host="localhost",user="user",passwd="gfg")# preparing a cursor objectcursorObject=dataBase.cursor()# creating databasecursorObject.execute("CREATE DATABASE geeks4geeks")
 

Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp