Skip to content

Latest commit

 

History

History
38 lines (24 loc) · 1.48 KB

how-do-i-access-mysql-using-python.md

File metadata and controls

38 lines (24 loc) · 1.48 KB

How do I access MySQL using Python?

// plain

To access MySQL using Python, you can use the mysql.connector library. This library allows you to connect to a MySQL database and perform various operations such as executing SQL statements, creating tables, and more.

Below is an example of how to connect to a MySQL database using Python:

import mysql.connector

# Establish a connection to the MySQL database
mydb = mysql.connector.connect(
    host="localhost",
    user="user",
    passwd="password"
)

# Print out the connection object
print(mydb)

The output of the above code will be a MySQLConnection object, which is used to perform operations on the MySQL database.

Code explanation

  • import mysql.connector: This imports the mysql.connector library, which allows you to connect to a MySQL database.

  • mydb = mysql.connector.connect(host="localhost", user="user", passwd="password"): This creates a connection to the MySQL database. The host, user, and passwd parameters are used to specify the hostname, username, and password, respectively.

  • print(mydb): This prints out the connection object, which is a MySQLConnection object.

Helpful links

onelinerhub: How do I access MySQL using Python?