WORKSHEET - 2 Interface Python With MySQL
WORKSHEET - 2 Interface Python With MySQL
1. The code given below reads the following record from the table named student and
displays only those records who have marks greater than 75.
Rollno - integer
Name - string
Class - integer
Marks - integer
Note the following to establish connectivity between Python and MYSQL:
Username is root
Password is tiger
The table exists in a MYSQL database named school
Write the following missing statements to complete the code:
Statement1 - To form the cursor object
Statement2 - To execute the query that extracts records of those students
whose marks are greater than 75.
Statement 3 - To read the complete result of the query(records whose marks
are greater than 75) into object named data, from the table
student in the database.
Import mysql.connector as mysql
def sql_data():
con1=mysql.connect(host=”localhost”,user=”root”,password=”tiger,database=”school”
)
mycursor=______________________ #statement 1
print(“Students with marks greater than 75 are :”)
_____________________________ #statement 2
data = _________________________ #statement 3
for i in data:
print(i)
print()
2. The code given below inserts the following record in the table student.
Rollno - integer
Name - string
Class - integer
Marks - integer
Note the following to establish connectivity between Python and MYSQL:
Username is root
Password is tiger
The table exists in a MYSQL database named school
The details (Rollno,Name, Class, and Marks) are to be accepted from the user.
Write the following missing statements to complete the code:
Statement 1: to form the cursor object
Statement 2: to execute the command that inserts the record in the table Student
Statement 3 : to add the record permanently in the database.
3. Write the Python code to create a table stationary in the database school with the
following table description
Fields Datatype
I_code Varchar(3)
I_Name char(30)
Qty int
Price Float
Note :
Host : localhost
Username : root
Password : pwdItem
4. Consider the table project of following structure storing the details of some ongoing
software projects.
ProjCode ProjName Team SubmissionDate
P01 Electrification 105 2023-12-05
Write the definition of a function ChangeData (Pcode, val) that will receive a project
code and a value and increase the “Team field by the value received, for those
projects whose projectCode matches with the Pcode received.
Note :
Database : ProjDB
Host : localhost
Username : root
Password : sqlpwd