9951 explained code solutions for 126 technologies


python-mysqlHow can I use the Python MySQL library to connect to a MySQL database?


To use the Python MySQL library to connect to a MySQL database, you need to first install the library and then import it into your code.

Example code

import mysql.connector

mydb = mysql.connector.connect(
    host="localhost",
    user="yourusername",
    passwd="yourpassword"
)

print(mydb)

Output example

<mysql.connector.connection.MySQLConnection object at 0x7f5f6d4f7d90>

The code above will:

  1. Import the MySQL library: import mysql.connector
  2. Establish a connection to the database using the mysql.connector.connect() method, which takes in parameters such as the host, user, and password.
  3. Print the connection object to the console.

Helpful links

Edit this code on GitHub