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.connector
library. - Create a
mydb
variable with themysql.connector.connect
method, passing in the host, user and passwd arguments. - Print the
mydb
variable.
Helpful links
More of Python Mysql
- How can I use the "order by" statement in Python to sort data in a MySQL database?
- How can I connect Python and MySQL?
- How do I connect to a MySQL database using XAMPP and Python?
- How do I check the version of MySQL I am using with Python?
- How do Python MySQL and SQLite compare in terms of performance and scalability?
- How can I install the MySQL-Python (Python 2.x) module?
- How can I resolve the "no database selected" error when using Python and MySQL?
- How do I use Python to update multiple columns in a MySQL database?
- How can I convert data from a MySQL database to XML using Python?
- How can I check the version of MySQL I'm using with Python?
See more codes...