Movatterモバイル変換


[0]ホーム

URL:


Open In App

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

Themysql.connector library provides theconnect() method, which is used to establish a connection between the MySQL database and a Python application. This allows you to connect to bothlocalandcloud-based MySQL databases effortlessly.

Syntax:

Conn_obj = mysql.connector.connect(
host=<hostname>,
user=<username>,
passwd=<password>,
database=<database_name> # optional

)

connect() function accepts the following arguments.

In the following example we will be connecting to MySQL database using connect() method.

1. Connecting to a Local MySQL Database

Here’s how to connect to a local MySQL database using themysql.connector.connect() method:

Python
importmysql.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:

Python
frommysql.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 theconnect() function using '**' operator:
Example:

Python
frommysql.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

Connecting to an Online MySQL Database

You can easily switch from connecting to a local MySQL database to an online MySQL database. All you need to do is modify the host parameter to theIP address orhostnameof your online database server.

Example

Python
importmysql.connectordataBase=mysql.connector.connect(host="your-cloud-database-host",# Replace with your cloud database hostuser="your-username",passwd="your-password",database="your-database-name")dataBase.close()

By changing the host value to point to the IP address or hostname of the cloud database, you can switch easily between a local and a cloud MySQL database without changing much of your code.


Improve
Improve
Article Tags :

Explore

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