INTERFACING PYTHON WITH MYSQL
Objective:
To write programs to connect python with mysql using database connectivity
and perform following operations on “EMPLOYEE” table in “BANK” database:
1. To display the names of all male staffs in employee table.
import [Link] as ms
mydb = [Link](host = “localhost”, user = “root”, passwd = “admin”, database =
“bank”)
if mydb.is_connected():
print(“Connection successful”)
mycursor = [Link]()
sq = “select ename from employee where gender = ‘{ }’ “ . format(‘M’)
[Link](sq)
data = [Link]()
for row in data:
print(row)
[Link]()
2. To insert new data into employee table:
import [Link] as ms
mydb = [Link](host = “localhost”, user = “root”, passwd = “admin”, database =
“bank”)
if mydb.is_connected():
print(“Connection successful”)
mycursor = [Link]()
sq = “insert into employee values ({ }, ‘{ }’, ‘{ }’, ‘{ }’, ‘{ }’, ‘{ } ‘, { })” . format(1234,
‘Riya’ , ‘IT’, ‘Delhi’, ‘F’, ‘1996-10-12’, 30000.00)
[Link](sq)
[Link]()
[Link]()
3. To update the salary of all employees of accounts department by incrementing
salary by 5000.
import [Link] as ms
mydb = [Link](host = “localhost”, user = “root”, passwd = “admin”, database =
“bank”)
if mydb.is_connected():
print(“Connection successful”)
mycursor = [Link]()
sq = “update employee set salary = salary + {} where dept = ‘{ }’ . format(5000, ‘IT)
[Link](sq)
[Link]()
[Link]()
4. To delete the records of all employees whose salary is less than 20000.
import [Link] as ms
mydb = [Link](host = “localhost”, user = “root”, passwd = “admin”, database =
“bank”)
if mydb.is_connected():
print(“Connection successful”)
mycursor = [Link]()
sq = “delete from employee where salary < 20000.00”
[Link](sq)
[Link]()
[Link]()