0% found this document useful (0 votes)
29 views

WORKSHEET - 2 Interface Python With MySQL

Uploaded by

Anisha Vetri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

WORKSHEET - 2 Interface Python With MySQL

Uploaded by

Anisha Vetri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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.

Import mysql.connector as mysql


def sql_data():
con1=mysql.connect(host=’localhost”,user =’root”, password=”tiger”,
database=”school”)
Mycursor =__________________________ #Statement 1
rno=int(input(“Enter Roll number :”))
name=input(“Enter name”)
class= int(input(“Enter Class”))
marks = int(input (“Enter Marks:”))
query=”insert into student values ({}.’{}’,{},{})”.format(rno,name,class,marks)
__________________________________ #Statement 2
__________________________________ #Statement 3
print(“Data Added Successfully”)

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

5. Consider the table Defence storing details of Defence personnel as pe following


structure :
Did Wing Rank Grade
Arm01 Army Major 3
Write a Python code to display the wind and grade of those personnel whose Rank is
“Major” or “Brigadier”

You might also like