Skip to content

Latest commit

 

History

History
38 lines (28 loc) · 1.12 KB

how-can-i-connect-python-and-mysql.md

File metadata and controls

38 lines (28 loc) · 1.12 KB

How can I connect Python and MySQL?

// plain

To connect Python and MySQL, you can use the mysql.connector module. This module provides an API which can be used to connect to a MySQL database.

Example code

import mysql.connector

# Connect to the database
conn = mysql.connector.connect(
    host="localhost",
    user="user",
    passwd="password",
    database="mydatabase"
)

# Print the connection object
print(conn)

Output example

<mysql.connector.connection_cext.CMySQLConnection object at 0x7f3c3f8f7e10>

The code above does the following:

  1. Imports the mysql.connector module.
  2. Connects to the database using the connect method. This requires passing in the host, user, password, and database name as parameters.
  3. Prints out the connection object.

Helpful links

onelinerhub: How can I connect Python and MySQL?