XII Practical Book Programs 18-21
XII Practical Book Programs 18-21
#Show Databases
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",password="angel")
mycursor=mydb.cursor()
mycursor.execute("show databases")
for x in mycursor:
print(x)
Output:
('information_schema',)
('hari',)
('hari1',)
('mysql',)
('performance_schema',)
('test',)
Program 19: Write a python program to connect with MySql and create employee table with
given information like employee no, employee name, department and salary.
Insert given values into employee table.
(101,'Manish Mehta','Sales',50000)
(102, ‘Satish Patel’, ‘Marketing’,45000)
(103, ‘Minesh Joshi’ , ‘Purchase’, 40000)
Code:
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",
password="angel",database='school')
mycursor=mydb.cursor()
mycursor.execute("create table employee(eno int primary key,ename varchar(30),
dept varchar(30),sal decimal)")
mycursor.execute("insert into employee values
(101,'Manish Mehta', 'Sales',50000)")
mycursor.execute("insert into employee values
(102,'Satish Patel', 'Marketing',45000)")
mycursor.execute("insert into employee values
(103,'Minesh Joshi','Purchase', 40000)")
mycursor.execute("commit")
print("Record Saved Successfully…!")
OUTPUT :
Record Saved Successfully…!
Program 20: Write a python program to connect with MySql and search record of given
employee number from employee table. If employee number is not present than display
message 'Record Not Found'.
Code:
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",
password="angel",database='school')
mycursor=mydb.cursor()
eno=int(input('Enter Employee Number to be Search:'))
mycursor.execute("Select * from employee where ENO=%s",(eno,))
rec=mycursor.fetchall()
if rec==[]:
print('Record Not Found...!')
else:
row=rec[0]
print('Employee No:',row[0])
print('Employee Name:',row[1])
print('Employee Dept:',row[2])
print('Employee Salary:',row[3])
OUTPUT :
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",
password="angel",database='school')
mycursor=mydb.cursor()
eno=int(input('Enter Employee Number to be Delete:'))
mycursor.execute("Delete from employee3 where ENO=%s",(eno,))
rec=mycursor.rowcount
if rec==0:
print('Record Not Found...!')
else:
mycursor.execute("commit")
print('Record Deleted Successfully...!')
mydb.close()
OUTPUT :