0% found this document useful (0 votes)
4 views5 pages

DBMS Exp11

The document outlines an experiment for connecting MySQL with Python as part of a Data Science course. It includes installation commands, a Python code snippet for establishing a database connection, executing a query, and handling exceptions. The conclusion emphasizes the understanding of database connectivity and CRUD operations using Python.

Uploaded by

abhisheksali18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

DBMS Exp11

The document outlines an experiment for connecting MySQL with Python as part of a Data Science course. It includes installation commands, a Python code snippet for establishing a database connection, executing a query, and handling exceptions. The conclusion emphasizes the understanding of database connectivity and CRUD operations using Python.

Uploaded by

abhisheksali18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Department of Computer Science and Engineering

Data Science
Academic Year: 2024-25 Name of Student: Abhishek Sali
Semester: IV Student ID: 23107058
Class / Branch: CSE (Data Science) Date of Performance: 01-04-2025
Subject:DBMS Date of Submission: 01-04-2025
Name of Instructor: Prof. Vaibhav Y

Experiment No. 11 Aim:

MYSQL connection with Python

Code:
sudo apt-get install mysql-server
sudo apt-get install python-dev libmysqlclient-dev sudo apt-
get install python-mysqldb sudo apt-get update
sudo apt-get install python3-dev libmysqlclient-dev sudo apt
install python3-pip sudo service mysql restart
import pymysql

# Database connection parameters


HOST = "localhost" # Hostname (e.g., localhost)
PORT = 3306 # MySQL default port
USER = "root" # MySQL username
PASSWORD = "123456" # MySQL password
DATABASE = "DB1" # MySQL database name

# Establish the connection try:


connection = pymysql.connect(
host=HOST, port=PORT,
user=USER,
password=PASSWORD,
database=DATABASE
)

print("Connection successful!")

# Use a context manager for the cursor with


connection.cursor() as cursor: # Execute a
simple query
cursor.execute("SELECT * FROM marks") # Replace 'marks' with your actual
table name

# Fetch all results results =


cursor.fetchall()

# Print the results for


row in results: print(row)

except pymysql.MySQLError as e:
print("MySQL Error: {}".format(e)) # More specific error message

except Exception as e: print("An error occurred: {}".format(e)) # Catch


any other exceptions

finally:
# Close the connection if it was established if
connection:
connection.close() print("Connection
closed.")
output:

Output:
Conclusion:
Thus, we have understood the concepts of database connectivity with python using CRUD
operations.

You might also like