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

12 (19-3-1) Ques Python SQL Interface

Uploaded by

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

12 (19-3-1) Ques Python SQL Interface

Uploaded by

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

(b) The code given below reads the following record from the table

“Library” and display only those records who have price less than 500:
BID – integer BTitle – string Pages – Integer Price – Integer
Note the following to establish connectivity between Python and MySQL:
- Username is root Password is kvs
- The table exists in MySQL database name LIB.
Write the following missing statements to complete the code:
Statement-1 : to import the appropriate library
Statement-2 : to form the cursor object
Statement-3: to execute the query that extracts records of those books whose Price is less
than 500
Statement-4 : to read the complete result of the query into the object data, from the table
Library in the database.
Statement-5 : to add the record permanently in the database
import _______________ #statment1
def sql_data( ):
con1=mc.connect(host=”localhost”, user=’root’, password=’kvs’, database=’LIB’)
mycur=______________ # Statement-2
print(“Books with price less than 500 are: ”)
_______________________# Statement-3
data =____________ # Statement-4
for i in data:
print(i)
______________________ # statement 5
Statement-1: con1.cursor()
Statement-2: mycur.execute(“select * from library where price < 500”)
Statement-3: mycur.fetchall( )
(b) The code given below reads the following record from the table
“Library” and display only those records who have price between 200 and 500:
BID – integer BTitle – string Pages – Integer Price – Integer
Note the following to establish connectivity between Python and MySQL:
- Username is root
- Password is kvs
- The table exists in MySQL database name LIB. 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 books whose Price is less
than 500
Statement-3 : to read the complete result of the query into the object data, from the table
Library in the database.

import mysql.connector as mc def sql_data( ):


con1=mc.connect(host=”localhost”, user=’root’, password=’kvs’, database=’LIB’)
mycur=_____________ # Statement-1
print(“Books with price less than 500 are: ”)
______________ # Statement-2
data =____________ # Statement-3
for i in data:
print(i)
print( )
The code given below reads the following record from the table named Books and displays
only those records who have Price greater than 200:Title – String AuthorName – string
ISBN_No – String Price – 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 Library. 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 books whose price are
greater than 200.
Statement 3- to read the complete result of the query (records whose price are greater than
200) into the object named data, from the table books in the database

import mysql.connector as mysql def sql_data():


con1=mysql.connect(host="localhost",user="root",password="tiger", database="library")
mycursor= #Statement 1
print("Books with Price greater than 200 are : ")
#Statement 2
data= #Statement 3
for i in data:
print(i)
print()
The code given below reads the following record from the table named Books and change
the author name from Alok to Aloknath.
Title – String AuthorName – string ISBN_No – String Price – 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 Library. 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 books whose price are
greater than 200.
Statement 3- to read the complete result of the query (records whose price are greater than
200) into the object named data, from the table books in the database

import mysql.connector as mysql def sql_data():


con1=mysql.connect(host="localhost",user="root",password="tiger", database="library")
mycursor= #Statement 1
print("Books with Price greater than 200 are : ")
#Statement 2
data= #Statement 3
for i in data:
print(i)
print()
(b) The code given below inserts the following record in the table Books: Title – String
AuthorName – string ISBN_No – String Price – 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 Library.
• The details (Title, AuthorName, ISBN_No and Price) 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 Library_data():
con1=mysql.connect(host="localhost",user="root",password="tiger", database="Library")
mycursor= #Statement 1 Title=input("Enter Book Title :: ")
AuthorName=input("Enter Book Author Name :: ") ISBN_No=input("Enter Book ISBN
Number:: ") Price=int(input("Enter Price of Book :: "))
querry="insert into Books values({},'{}',{},{})".format(Title,AuthorName , ISBN_No,Price)
#Statement 2
# Statement 3 print("Data Added successfully")
b)
Statement 1: con1.cursor()
Statement 2: mycursor.execute(querry)
Statement 3: con1.commit()
b) The code given below extract and display the following record from the table
Employee.
Note the following to establish connectivity between Python and MYSQL: Username is root
Password is computer
The table exists in a MYSQL database named Employee.
The details (Emp_id, Ename, Date_of_Birth, Salary) are present in table. Write the following
missing statements to complete the code:
i. Statement 1 – to form the cursor object
ii. Statement 2 – to retrieve all records from result set object as per query.
iii. Statement 3 - sequence object in the loop to display the records

import mysql.connector as mycon def ShowRecord():


mydb=mycon.connect(host="localhost", user="root", password= "computer",
database="bank")
mycursor= #Statement
qry="select * from employee where salary>30000"
mycursor.execute(qry)
result=mycursor.___________ #Statement 2
for record in __________ : #Statement 3
print(record)
b) The code given below inserts the following record in the table Employee: Emp_id –
integer
Ename – string
Date_of_Birth – date
Salary – float
Note the following to establish connectivity between Python and MYSQL: Username
is root
Password is computer
The table exists in a MYSQL database named Employee.
The details (Emp_id, Ename, Date_of_Birth, Salary) are to be accepted from the user.
Write the following missing statements to complete the code:
i. Statement 1 – to form the cursor object
ii. Statement 2 – to execute the command that inserts the record in the table
Employee.
iii. Statement 3- to add the record permanently in the database
import mysql.connector as mycon def AddRecord():
mydb=mycon.connect(host="localhost",user="root", password= "computer",
database="bank")
mycursor= #Statement 1 Emp_id=int(input("Enter Employee
Id: ")) Ename=input("Enter Employee Name : ") dob=input("Enter Date of
Birth : ") salary=float(input("Enter Salary : "))
qry="insert into Employee values ({},'{}','{}',{})".format (Emp_id, Ename, dob,
salary)
#Statement 2
# Statement 3
print("Data Added successfully")
Q.1 The code given below inserts the following record in table Garment: GCode – integer
GName – string Rate – decimal Qty – integer CCode = integer
Write missing Statements (Statement 1, Statement 2 and Statement 3) to complete
the code:

import mysql.connector as mycon


cn=mycon.connect(host=’localhost’,user=’test’,password=’None’, database=’cloth’)
cur = _______________________ #Statement 1
while True:
GCode = int(input(“Enter Garment Code = “))
GName = input(“Enter Garment Name = “)
Rate = float(input(“Enter Rate = “))
Qty = int(input(“Enter Quantity = “))
CCode = int(input(“Enter Cloth Code = “))
q = “insert into Garment Values ({}, ‘{}’, {}, {}, {}”.format(GCode,GName, Rate, Qty, CCode)
cur.______________(q) #Staatement 2
cur.______________ #Statement 3
ans = input(“Do you have more records (Y/N)=”)
if ans.upper()==’N’:
break
print(“Records Successfully Saved”)
cn.close()
Ans.1 Statement 1: cur = cn.cursor()
Statement 2: cur.execute(q)
Statement 3: cn.commit()
(b) The code given below inserts the following record in the table “Library”:
BID – integer BTitle – string Pages – Integer Price – Integer
Note the following to establish connectivity between Python and MySQL:
- Username is root
- Password is kvs
- The table exists in MySQL database name LIB.
- The details (BID, BTitle, Pages, Price) are to be accepted from 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 record in table Statement-3 : to add
the record permanently in the database

import mysql.connector as mc
def sql_data( ):
con1=mc.connect(host=”localhost”, user=’root’, password=’kvs’, database=’LIB’)
mycur=________________ # Statement-1
BID=int(input(‘Enter book ID=’)) BTitle=input(‘Enter book title=’)
Pages = int(input(“Enter no. of pages=”)) Price = int(input(“Enter book price=”))
Qry=”insert into library values ( { }, ‘{ }’, { }, { })”. format(BID,
BTitle, Pages, Price)
___________________# Statement-2
___________________# Statement-3
print(‘Data added in the table”)
Ans:
Statement-1: con1.cursor()
Statement-2: mycur.execute(Qry)
Statement-3: con1.commit()
(b) The code given below inserts the following record in the table Books: Title – String
AuthorName – string ISBN_No – String Price – 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 Library.
• The details (Title, AuthorName, ISBN_No and Price) 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 Library_data():
con1=mysql.connect(host="localhost",user="root",password="tiger", database="Library")
mycursor=___________ #Statement 1
Title=input("Enter Book Title :: ")
AuthorName=input("Enter Book Author Name :: ")
ISBN_No=input("Enter Book ISBN Number:: ")
Price=int(input("Enter Price of Book :: "))
querry="insert into Books values({},'{}',{},{})".format(Title,AuthorName , ISBN_No,Price)
______________ #Statement 2
_______________# Statement 3
print("Data Added successfully")
(b) The code given below delete the record whose price are
greater than 300 from the table “Library”:
BID – integer BTitle – string Pages – Integer Price – Integer
Note the following to establish connectivity between Python and
MySQL:
- Username is root
- Password is kvs
- The table exists in MySQL database name LIB.
- The details (BID, BTitle, Pages, Price) are to be accepted from
user
Write the following missing statements to complete the code:
Statement-1 : to form the cursor object
Statement-2 : to execute the command that delete record in table
Statement-3 : to add the record permanently in the database
import mysql.connector as mc def sql_data( ):
con1=mc.connect(host=”localhost”, user=’root’, password=’kvs’, database=’LIB’)
mycur=con1.cursor() # Statement-1
price = int(input(“Enter book price greater than”))
qry=”delete from library where price> { }”. format(price)
mycur.execute(qry) # Statement-2

Con1.commit() # Statement-3
print(‘Data added in the table”)

You might also like