Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
101 views

Interfacing Python With MySql

Uploaded by

Aslam Basha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views

Interfacing Python With MySql

Uploaded by

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

Database Concept -SQL

Worksheet - 4
SUB: COMPUTER SCIENCE (083)
DATE : 21.9.2024
SNO QUESTIONS
1. In order to open a connection with MySQL database from within Python using mysql.connector
package, __________ function is used.
(A) open (B) connect (C) database() (D) connectdb()
2. The code given below inserts the following record in the table Student:

Note the following to establish connectivity between Python and MySQL:


* Username is root
* Password is toor@123
*The table exists in a “stud” database.
* The details (RollNo, Name, Clas 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 sqlData():
con1=mysql.connect(host="localhost",user="root", password="toor@123", database="stud")
mycursor = ________________ #Statement 1
rno=int(input("Enter Roll Number :: "))
name=input("Enter name :: ")
clas=int(input("Enter class :: "))
marks=int(input("Enter Marks :: "))
querry="insert into student values({},'{}',{},{})".format(rno,name,clas,marks)
______________________ #Statement 2
______________________ # Statement 3
print("Data Added successfully")
3. The code given below reads the following record from the table named student and displays only
those records who have marks greater than 90:

Note the following to establish connectivity between Python and MySQL:


* Username is root
* Password is toor@123
* The table exists in a “stud” database.
Write the following missing statements to complete the code:
Statement 1 – to form the cursor object
Statement 2 – to execute the query that extracts records of those students whose marks are greater
than 90. Statement 3- to read the complete result of the query (records whose marks are greater than
90) into the 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="toor@123", database="stud")
mycursor=_______________ #Statement 1
print("Students with marks greater than 90 are : ")
______________________ #Statement 2
data=__________________ #Statement 3
for i in data:
print(i)
print()
4. Which of the following is not valid cursor function while performing database operations using
python. Here Mycur is the cursor object?
a) Mycur.fetch() b) Mycur.fetchone() c) Mycur.fetchmany(n) d) Mycur.fetchall()
5. Sumitra wants to write a program to connect to MySQL database using python and increase the age of
all the students who are studying in class 11 by 2 years. Since she had little understanding of the
coding, she left a few blank spaces in her code. Now help her to complete the code by suggesting
correct coding for statements 1, 2 and 3.
import ________________ as myc # Statement 1
con = myc.connect(host="locahost", user="root", passwd="", database="mydb")
mycursor = __________________ #Statement 2
sql = "UPDATE student SET age=age+2 WHERE class='XI'"
mycursor.execute(sql)
sql = "SELECT * FROM student" mycursor=con.execute(sql)
result = _____________________ #Statement 3
for row in result:
print(row)
Statement 1 : The required module to be imported
Statement 2: To initialize the cursor object.
Statement 3: To read all the rows from the cursor object
6. Which of the following is not a legal method for fetching records from a database from within a
Python program? (a) fetchone() b)fetchtwo() (c) fetchall() (d) fetchmany()
7. The statement which is used to get the number of rows fetched by execute() method of cursor:
(a) cursor.rowcount (b) cursor.rowscount() (c) cursor.allrows() (d) cursor.countrows()
8. The code given below adds a new column in the table Student, updates the data into it and displays
the content of the table. Student table details are as follows:
Rollno – integer Name – string Age – integer
Note the following to establish connectivity between Python and MYSQL:
• Username is root
• Password is sys
• The table exists in a MYSQL database named school.
Write the following missing statements to complete the code:
Statement 1 – to form the cursor object
Statement 2 – to execute a query that adds a column named “MARKS” of type integer in the table
Student. Statement 3- to update the record permanently in the database
import mysql.connector
mydb= mysql.connector.connect(host="localhost",user="root",passwd="sys", database="myschool")
mycursor = _____________________ #statement1
________________________________ #statement2
mycursor.execute("update student set MARKS=9 where Rollno = 1")
______________________________ #statement3
mycursor.execute("select * from student")
for x in mycursor:
print(x)
9. Which of the following function is used to established connection between Python and MySQL
database –
(a) connection() (b) connect() (c) Connect() (d) None
10. The code given below inserts the following record in the table EMP:
EmpID – integer
Name – string
Salary – integer
Note the following to establish connectivity between Python and MYSQL:
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 EMP.
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="kvs", database="KVS")
mycursor=_________________ #Statement 1
eno=int(input("Enter Employee ID : "))
name=input("Enter name : ")
sal=int(input("Enter Salary : "))
querry="insert into EMP values({},'{}',{})".format(eno,name,sal)
______________________ # Statement 2
______________________ # Statement 3
print("Data Added successfully")
11. Write a python program to display all records in ascending order of their salary from employee table.
12. Write a program to delete the employee record whose name is read from keyboard at execution time.
13. Write a program to create a table Emloyee with fields First_Name, Last_Name, Age,Gender nd Income.
14. Write a Python connectivity program to retrieve data one record at a time from EMP table for
employees with id<0.
15. Write code to connect Python with MySql using table Members present in the database ‘Society’ and
count the number of Members id the table where the id columns equals 1.

You might also like