9951 explained code solutions for 126 technologies


python-mysqlHow do I connect to a MySQL database using XAMPP and Python?


To connect to a MySQL database using XAMPP and Python, you will need to create a connection object, using the mysql.connector module. The following example code will connect to a database named mydatabase on a localhost server:

import mysql.connector

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

print(mydb)
# Output: <mysql.connector.connection.MySQLConnection object at 0x7f3a1cf2f3d0>

The code consists of the following parts:

  1. import mysql.connector: This imports the mysql.connector module, which is used to connect to a MySQL database.
  2. mydb = mysql.connector.connect(): This creates a connection object, which is used to connect to the database.
  3. host="localhost": This specifies the hostname of the server, which in this case is localhost.
  4. user="yourusername": This specifies the username used to access the database.
  5. passwd="yourpassword": This specifies the password used to access the database.
  6. database="mydatabase": This specifies the name of the database to connect to.
  7. print(mydb): This prints the connection object, which indicates that the connection was successful.

For more information, see the MySQL Connector/Python documentation.

Edit this code on GitHub