How to use the PyMySQL library in Python

1. Install the PyMySQL library using pip:

pip install PyMySQL

2. Import the PyMySQL library in your Python script:

import pymysql

3. Connect to a MySQL database:

conn = pymysql.connect(host=”localhost”, user=”username”, password=”password”, db=”database_name”)

4. Create a cursor object to execute queries:

cur = conn.cursor()

5. Execute the query:

cur.execute(“SELECT * FROM table_name”)

6. Fetch the results:

rows = cur.fetchall()

7. Close the connection: