python-mysqlHow do I connect to XAMPP MySQL using Python?
To connect to XAMPP MySQL using Python, you need to use the mysql.connector library.
A basic example of connecting to a local MySQL database with mysql.connector would be:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
passwd="yourpassword"
)
print(mydb)
The output of the above code would be something like:
<mysql.connector.connection_cext.CMySQLConnection object at 0x7f8f2b7f8e10>
The code consists of the following parts:
- Import the
mysql.connectorlibrary. - Create a
mydbvariable with themysql.connector.connectmethod, passing in the host, user and passwd arguments. - Print the
mydbvariable.
Helpful links
More of Python Mysql
- How do I use Python to handle MySQL NULL values?
- How can I connect to MySQL using Python?
- How can I access MySQL using Python?
- How do I download MySQL-Python 1.2.5 zip file?
- How can I connect Python to a MySQL database?
- How can I use Python and MySQL to generate a PDF?
- How do I connect Python with MySQL using XAMPP?
- How can I troubleshoot a Python MySQL OperationalError?
- How to compile a MySQL-Python application for x86_64-Linux-GNU-GCC?
- How do I use an online compiler to write Python code for a MySQL database?
See more codes...