0% found this document useful (0 votes)
44 views3 pages

MySQL Python Connection and Queries Guide

The document is a SQL worksheet with multiple sections covering connecting to a MySQL database using Python, SQL queries for specific operations on tables, and Python programs for database manipulation. It includes definitions, explanations of methods, and practical coding tasks related to database operations. The worksheet is structured into sections with varying marks, indicating its use as an educational resource.

Uploaded by

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

MySQL Python Connection and Queries Guide

The document is a SQL worksheet with multiple sections covering connecting to a MySQL database using Python, SQL queries for specific operations on tables, and Python programs for database manipulation. It includes definitions, explanations of methods, and practical coding tasks related to database operations. The worksheet is structured into sections with varying marks, indicating its use as an educational resource.

Uploaded by

jokerbloody0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SQL

WORKSHEET – 4
SECTION A(5 marks)
1. Define the steps to connect to a MySQL database using python.
2. Explain the purpose of the COMMIT() and ROLLBACK() methods in database operations.
3. What is the difference between FETCHONE() and FETCHALL() methods in python’s MySQL connector?
4. Describe the role of the cursor object in executing SQL queries.
5. What is the significance of using parameterized queries in python-MySQL connectivity?

SECTION B(15 marks)


6. Given the tables ‘DOCTOR’ and ‘SALARY’, write SQL queries to:
a. Display the names of all doctors in the ‘MEDICINE’ department with more than 10 years of
experience.
b. Calculate the average salary of doctors in the ‘ENT’ department, where salary = basic +
allowance.
c. Find the minimum allowance among female doctors.
d. List the names, basic salary, allowance, and consultation fee of male doctors in the
‘ORTHOPAEDIC’ department.
7. Write a SQL query to update the percentage of a student with RollNo 15 by increasing it by 10% in the
‘College’ table.
8. Write a SQL query to delete a record from the ‘Club’ table where the MemberName is ‘Sachin’.

SECTION C(20 marks)


9. Write a python program to connect to a MySQL database named ‘School’ and fetch all records from the
‘Student’ table where the grade is ‘A’.
10. Write a python program to insert a new record into the ‘Student’ table with the following details:
 RollNo: 106
 Name: ‘Arsiya’
 Marks: 91.6
 Grade: ‘A+’
 Section: ‘B’
 Project: ‘SUBMITTED’
11. Write a python program to update the marks of a student with RollNo 10 to 95 in the ‘Student’ table.
12. Write a python program to fetch and display all records from the ‘Student’ table where the marks>75.

SECTION D(10 marks)


13. Write a python function addcsv() to append records from [Link] to [Link].
14. Write a python function convertcsv() to convert the delimiter of [Link] to ‘;’ and save it as
[Link].

SECTION E(10 marks)


15. Write a python program to connect to a MySQL database named ‘System’ and delete a record from the
‘Club’ table where the MemberName is ‘Sachin’.
16. Write a python program to update the percentage of a student with RollNo 15 by increasing it by 10% in
the ‘College’ table.
ANSWERS
1. Step 1: install the [Link] package using ‘pip’
Step 2: import the module [Link], use aliasing for saving time and type less
Step 3: create the connection and cursor objects. While creating the connection object we use keyword
arguments and write the name of database to connect.

2. COMMIT() is used to save the changes made by the query executed permanently.
ROLLBACK() is used to undo the changes made by the query. It is used when user by mistake adds
incorrect data or error occurred then they may use this function to revert the changes.

3. FETCHONE() fetches one row at a time. It is mainly used in loops to access a record one-by-one. It
outputs a tuple.
FETCHALL() fetches all records at once and displays a list of tuples where each tuple is a row of the
table. It is used when you want retrieve everything in a single object/variable.

4. CURSOR is the object that acts as bridge between python and SQL. We write code in python which is
then sent to a SQL server and get run there. Then it grabs the result and then the user can retrieve the
resultant set of the query.

5. Parameterized queries are the queries in which instead of giving actual data we give placeholders and
then the data is automatically formatted to the appropriate datatype and inserted in the place of
placeholders. They automatically format the data and also more secure since it prevents SQL injection.

6. a. SELECT Name FROM DOCTOR WHERE Department=‘MEDICINE’ AND Exp_years>10;


b. SELECT AVG(Basic + Allowance) AS ‘Avg Salary’ FROM DOCTOR d INNER JOIN SALARY s ON [Link]=[Link]
WHERE Department = ‘ENT’;
c. SELECT MIN(Allowance) AS min_allowance FROM DOCTOR d JOIN SALARY s ON [Link] = [Link] WHERE
[Link] = 'Female';
d. SELECT [Link], Basic, Allowance, Consultation FROM DOCTOR d JOIN SALARY s ON [Link]=[Link] WHERE
gender=’Male’ AND department=’ORTHOPAEDIC’;

7. UPDATE College SET Percentage=Percentage*1.1 WHERE RollNo=15;

8. DELETE FROM Club WHERE MemberName=‘Sachin’;

9. import [Link] as m
connection=[Link](user=‘root’, host=‘localhost’, password=‘123’, database=‘School’)
cursor=[Link]()
[Link](‘SELECT * FROM Student WHERE grade="A"‘)
a=[Link]()
for i in a: print(a)
[Link]()
[Link]()
10. import [Link] as m
connection=[Link](user=‘root’, host=‘localhost’, password=‘123’, database=‘xyz’)
cursor=[Link]()
[Link]("INSERT INTO Student (RollNo, Name, Marks, Grade, Section, Project) VALUES (%s, %s,
%s, %s, %s, %s)", (106, ‘Arsiya’, 91.6, ‘A+’, ‘B’, ‘SUBMITTED’))
[Link]()
[Link](‘SELECT * FROM Student’)
a=[Link]()
for i in a: print(i)
[Link]()
[Link]()

11. import [Link] as m


connection=[Link](user=‘root’, host=‘localhost’, password=‘123’, database=‘xyz’)
cursor=[Link]()
[Link]("UPDATE Student SET Marks=%s WHERE RollNo=%s", (95, 10))
[Link]()
[Link]("SELECT * FROM Student WHERE RollNo=%s", (10,))
record=[Link]()
print("Updated Record:", record)
[Link]()
[Link]()

12. import [Link] as m


connection=[Link](user=‘root’, host=‘localhost’, password=‘123’, database=‘xyz’)
cursor=[Link]()
[Link](‘SELECT * FROM Student WHERE Marks>75’)
a=[Link]()
for i in a: print(i)
[Link]()
[Link]()
13. WTH
14. WTH
15. import [Link] as m
connection=[Link](user=‘root’,host=‘localhost’,password=‘123’,database=‘System’)
cursor=[Link]()
[Link]("DELETE FROM Club WHERE MemberName=%s",(‘Sachin’,))
[Link]()
[Link]()
[Link]()

16. import [Link] as m


connection=[Link](user=‘root’,host=‘localhost’,password=‘123’,database=‘xyz’)
cursor=[Link]()
[Link]("UPDATE College SET Percentage=Percentage*1.1 WHERE RollNo=%s",(15,))
[Link]()
[Link]()
[Link]()

You might also like