Professional Documents
Culture Documents
NAME:Hariharan.S
Q1.
Source code
import mysql.connector
def input_values():
L=[]
empno=int(input("Enter the employee number:"))
ename=input("Enter the name of the employee :")
dep=input("Enter the department id of the employee :")
L.append(empno)
L.append(ename)
L.append(dep)
emp=(L)
sql="insert into employee (empno,ename,deptid) values (%s,%s,%s)"
cur.execute(sql,emp)
c.commit()
def view_values():
print("Select the search criteria : ")
print("1. Emp no")
print("2. Name")
print("3. Department")
print("4. Display all records")
ch=int(input("Enter the choice : "))
if ch==1:
s=int(input("Enter employee no : "))
rl=(s,)
sql="select * from employee where empno=%s"
cur.execute(sql,rl)
elif ch==2:
s=input("Enter Name : ")
rl=(s,)
sql="select * from employee where ename=%s"
cur.execute(sql,rl)
elif ch==3:
s=int(input("Enter department id : "))
rl=(s,)
sql="select * from employee where deptid=%s"
cur.execute(sql,rl)
elif ch==4:
sql="select * from employee"
cur.execute(sql)
else:
print("Invalid option")
res=cur.fetchall()
print("The employee details are as follows : ")
print("(Empno, Name, deptid)")
for x in res:
print(x)
def MenuSet():
print("Enter 1 : To Add employee")
print("Enter 2 : To View employee details")
p=[]
c=mysql.connector.connect(host="localhost",user="root",passwd="Tvsahosur$123")
cur=c.cursor()
db=input("Enter the name of the database :")
p.append(db)
st="create database if not exists "+db
cur.execute(st)
if c.is_connected():
print("Database connected successfully")
st="use "+db
cur.execute(st)
st="create table IF NOT EXISTS employee(empno int,ename varchar(20),deptid int)"
cur.execute(st)
print("employee table created successfully")
print()
ch='y'
while(ch=='y'):
MenuSet()
print()
ch=input("Do you want to continue?y/n")
if ch.lower()=='y':
continue
else:
break
OUTPUT:
Enter the name of the database :employee
Database connected successfully
employee table created successfully
#a) List the name of all students who have taken stream as COMPUTER SCIENCE
select * from student where stream='Computer Science';