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

Python-MySQL Connectivity Worksheet

Uploaded by

muniraj46567
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)
12 views3 pages

Python-MySQL Connectivity Worksheet

Uploaded by

muniraj46567
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

Grade XII – Computer Science

Python–MySQL Connectivity – Revision Worksheet

Part A – Theory (1 mark each)

Q1. Write the name of the Python module used to connect with MySQL.

Q2. Arrange the following steps of Python–MySQL connectivity in correct order:


a. Execute SQL statements
b. Close connection
c. Import module
d. Create cursor object
e. Establish connection

Q3. Name the method used to fetch:


(a) One row from a query result
(b) All rows from a query result

Q4. What is the purpose of commit() method in MySQL–Python connectivity?

Part B – Output Prediction (2 marks each)

Q5.

import [Link]
con = [Link](host="localhost", user="root", passwd="1234",
database="school")
cur = [Link]()
[Link]("SELECT Name FROM student WHERE Marks > 80")
rows = [Link]()
for r in rows:
print(r[0])

If table student contains:

RollNo Name Marks


1 Ajay 78
2 Sneha 84
3 Ritu 90

Output:
Q6.

import [Link]
con = [Link](host="localhost", user="root", passwd="1234",
database="company")
cur = [Link]()
[Link]("SELECT * FROM emp WHERE Salary BETWEEN 30000 AND 40000")
print([Link]())

If table emp contains:

EID Name Salary


101 Rahul 35000
102 Meena 42000
103 Arjun 39000

Output:

Part C – Error Finding & Correction (2 marks each)

Q7.

import [Link]
mycon = [Link]("localhost", "root", "1234", "school")
cur = [Link]()

Q8.

[Link]("SELECT Name Salary FROM emp")

Part D – Program Writing (3 marks each)

Q9. Write a Python program to insert a record (105, 'Neha', 47000) into table emp(EID
INT, Name VARCHAR(20), Salary INT) in database company.

Q10. Write a Python program to display the Name and Salary of all employees whose salary
is more than 50000 from table emp.

Q11. Write a Python program to update the salary of employee with EID = 103 to 55000 in
table emp.
Part E – Integrated Board-style Questions (4–5 marks each)

Q12. A database school has a table student as follows:

RollNo Name Marks


1 Amit 85
2 Priya 78
3 Rohan 92
4 Neha 88

Write a Python program to:

1. Connect to MySQL
2. Display names of students scoring between 80 and 90

Q13. The table book in database library has:

BID Title Author


1 The Alchemist Paulo Coelho
2 Wings of Fire A P J Abdul Kalam
3 Python Magic Rahul Sharma

Write a Python program to:

 Insert a new record (4, 'Think Python', 'Allen Downey')


 Display all records from book table

Common questions

Powered by AI

Python's error handling, using `try-except` blocks, is crucial in MySQL connectivity to catch and handle exceptions like connection errors, invalid credentials, SQL syntax errors, or data type mismatches. For instance, wrapping database operations within try-except can gracefully terminate processes and log errors instead of crashing the application, maintaining stability and aiding in debugging .

Changing the selection criteria in an SQL query affects the program’s output by altering which rows from the database are included in the result set. For example, using 'SELECT * FROM emp WHERE Salary BETWEEN 30000 AND 40000' will only include employees with salaries within this range. Any alterations in the criteria, such as changing salary limits or including additional conditions, will result in different subsets of data being retrieved and displayed .

The Python module commonly used to connect with a MySQL database is mysql.connector. It is preferred because it is a robust and widely-used library compliant with the Python Database API Specification v2.0 and offers consistent functionalities for managing MySQL connections and executing SQL queries .

A Python script to update a record's salary in table emp would include: 1. Importing mysql.connector and establishing a connection using mysql.connector.connect() with necessary parameters. 2. Creating a cursor object. 3. Executing an update query like 'UPDATE emp SET Salary = 55000 WHERE EID = 103'. 4. Using con.commit() to apply the changes persistently. 5. Finally, closing the connection with con.close() to release resources and end the session .

If the student table contains rows with names and marks as provided, and the script executes a query with 'SELECT Name FROM student WHERE Marks > 80', the output will include the names of students who have more than 80 marks. Specifically, it will output 'Sneha' and 'Ritu' since their marks are 84 and 90, respectively . If the data in 'Marks' changes, the output will vary accordingly, showing the names that satisfy the condition in the WHERE clause.

A Python program to insert a record (105, 'Neha', 47000) into a table emp involves: 1. Importing mysql.connector and establishing a connection using mysql.connector.connect() with the appropriate host, user, passwd, and database parameters. 2. Creating a cursor object. 3. Executing the query 'INSERT INTO emp (EID, Name, Salary) VALUES (105, "Neha", 47000)' using cur.execute(). 4. Committing the transaction with con.commit() to save the changes. 5. Closing the connection with con.close().

The fetchone() method retrieves the next row of a query result set, returning a single tuple, which is useful when only one row is needed, resulting in lower memory usage. On the other hand, fetchall() retrieves all the remaining rows of the query result, returning a list of tuples, making it suitable when all data is required but can consume more memory .

The correct order of steps for establishing and using a Python-MySQL connection are: c. Import module, e. Establish connection, d. Create cursor object, a. Execute SQL statements, b. Close connection .

The commit() method in MySQL-Python connectivity is used to permanently save all the changes made during the current database session. Without calling commit(), any changes like data inserts, updates, or deletions are not written to the database, and they will be lost once the session ends .

In the statement 'mycon = mysql.connector.connect("localhost", "root", "1234", "school") cur = mycon.Cursor()', the errors rest in the connect syntax and the Cursor method. The correct connection line should be 'mycon = mysql.connector.connect(host="localhost", user="root", passwd="1234", database="school")'. The 'Cursor()' method needs to be lowercase: 'cur = mycon.cursor()' .

You might also like