12 (19-3-1) Ques Python SQL Interface
12 (19-3-1) Ques Python SQL Interface
“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
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”)