python-mysqlHow do I use Python to connect to a MySQL database?
Connecting to a MySQL database with Python is a straightforward process. First, you must install the mysql-connector-python
package using the pip
command.
pip install mysql-connector-python
Once installed, you can use the mysql.connector.connect()
method to connect to the database. You must provide the host
, user
, password
, and database
parameters.
import mysql.connector
db = mysql.connector.connect(
host="localhost",
user="username",
password="password",
database="database"
)
Once connected, you can use the cursor()
method to create a cursor object for executing SQL queries.
cursor = db.cursor()
You can then use the execute()
method of the cursor object to execute SQL queries.
cursor.execute("SELECT * FROM table")
for row in cursor.fetchall():
print(row)
Output example
(1, 'John', 'Doe')
(2, 'Jane', 'Doe')
For more information, see the MySQL Connector/Python documentation.
More of Python Mysql
- How can I connect Python to a MySQL database?
- How do I connect Python with MySQL using XAMPP?
- How can I use Yum to install the MySQLdb Python module?
- How do I use Python to query MySQL with multiple conditions?
- How do I use Python to access MySQL binlogs?
- How do I use Python to authenticate MySQL on Windows?
- How can I use Python to retrieve data from MySQL?
- How do I use Python to handle MySQL NULL values?
- How can I use Python and MySQL to generate a PDF?
- How do I connect to a MySQL database using XAMPP and Python?
See more codes...