Interface Python
Interface Python
1. Design a Python application that fetches all the records from Pet table of menagerie database.
Ans:
import mysql.connector as a
mydb = a.connect(host="localhost",user="root",password="user", database = "menagerie")
cur = mydb.cursor()
run = "select * from PET "
cur . execute(run)
data = cur.fetchall()
for i in data :
print(i)
mydb.close()
2. Design a Python application that fetches only those records from Event table of menagerie
database where type is Kennel.
Ans:
import mysql.connector as a
mydb = a.connect(host="localhost",user="root",password="user", database = "menagerie")
cur = mydb.cursor()
run = "select * from Event where type = 'Kennel' "
cur . execute(run)
data = cur.fetchall()
for i in data :
print(i)
mydb.close()
3. Design a Python application to obtain a search criteria from user and then fetch records based
on that from empl table.
Ans:
import mysql.connector as a
mydb = a.connect(host="localhost",user="root",password="user", database = "portal_express")
name = input("Enter the employee name :- ")
cur = mydb.cursor()
run = "select * from Empl where name = '{}' ".format(name,)
cur . execute(run)
data = cur.fetchall()
for i in data :
print(i)
mydb.close()
4. Write code to connect to a MYSQL database namely School and then fetch all those records from
table Student where grade is 'A'.
Ans:
import mysql.connector as a
mydb = a.connect(host="localhost",user="root",password="portal express", database = "School")
cur = mydb.cursor()
run = "select * from Student where grade = 'A' "
cur . execute(run)
data = cur.fetchall()
for i in data :
print(i)
mydb.close()
16. Which function is used to read one record from the database?
Ans: fetchone() function.
17. Write a program to display all records in ascending order of their salary.
Ans:
import mysql.connector as a
mydb = a.connect(host="localhost",user="root",password="user", database = " abc ")
cur = mydb.cursor()
run = "select * from emp order by salary "
cur . execute(run)
data = cur.fetchall()
for i in data :
print(i)
mydb.close()
18. Write a program to increase salary of the employee, whose name is "MANOJ KUMAR", by 3000.
Ans:
import mysql.connector as a
db = a.connect(host="localhost",user="root",password="user", database = "abc")
cursor = db.cursor()
cursor.execute("update emp set salary = salary + 3000 where name = 'MANOJ KUMAR' ")
db.commit()
db.close()
19.Write a program to delete the employee record whose name is read from keyboard at execution
time.
Ans:
import mysql.connector as a
db = a.connect(host="localhost",user="root",password="user", database = "abc")
cursor = db.cursor()
name = input("Enter Employee name = ")
cursor.execute("delete from emp where name = { } ".format(name))
db.commit()
db.close()
20. Create a database TESTDB.
Create a table EMPLOYEE with Fields FIRST_NAME, LAST_NAME, AGE, GENDER and
INCOME in TESTDB.
Ans:
import mysql.connector as a
db = a.connect(host="localhost",user="root",password="user", database = "TESTDB")
cursor = db.cursor()
cursor.execute("Create table EMPLOYEE ( FIRST_NAME char(50), LAST_NAME char(50), AGE
integer, GENDER char(1) , INCOME integer )")
db.commit()
db.close()
***************