You are on page 1of 4

*******************NAME-ANIKET.

K - I2- 39******************

Experiment No.: 11 C

Aim: Write Python program to understand database connectivity with DML.

Program Code:
import mysql.connector
db= mysql.connector.connect(host="localhost", user="aniket",
passwd="password123")
cursor1= db.cursor()
print(db)
cursor1.execute("create database company ")
cursor1.execute("show databases")
print("Databases: ")
for r in cursor1:
print(r)
db1= mysql.connector.connect(host="localhost", user="aniket",
passwd="password123",database="company")
cursor2=db1.cursor()
cursor2.execute("create table employee(Name varchar(20),Age int(10),Salary
int(20))")
print()
print("Tables: ")
cursor2.execute("show tables")
for r in cursor2:
print(r)
a="insert into employee(Name,Age,Salary) values(%s,%s,%s)"
b=[('Ajay',24,10000),('Sonali',22,15000),('Raj',25,20000),('Diya',24,10000),
('Ganesh',23,15000)]
cursor2.executemany(a,b)
db1.commit()
cursor2.execute("select * from employee")
print()
print("Table contain: ")
m=cursor2.fetchall()
for r in m:
print(r)
cursor2.execute("select name,age from employee where salary=10000")
print()
print("Employees having salary=10000 :")
m=cursor2.fetchall()
for r in m:
print(r)
cursor2.execute("select * from employee where name like 's%'")
print()
print("Employee name starting with 's':")
m=cursor2.fetchall()
for r in m:
print(r)
cursor2.execute("update employee set salary=salary+5000 where salary>=15000")
db1.commit()
cursor2.execute("select * from employee ")
print()
print("Table contain after updation in salary : ")
m=cursor2.fetchall()
for r in m:
print(r)
cursor2.execute("select * from employee limit 3")
print()
print("First 3 rows of table: ")
m=cursor2.fetchall()
for r in m:
print(r)
cursor2.execute("select * from employee order by name")
print()
print("Table contain in ascending order by name: ")
m=cursor2.fetchall()
for r in m:
print(r)
cursor2.execute("select * from employee order by age desc")
print()
print("Table contain in descending order by age: ")
m=cursor2.fetchall()
for r in m:
print(r)
cursor2.execute("delete from employee where age=22")
print()
cursor2.execute("select * from employee ")
print("Table contain after delete operation: ")
m=cursor2.fetchall()
for r in m:
print(r)
cursor2.execute("drop table employee")
db1.commit()
cursor2.close()
db1.close()
cursor1.execute("drop database company ")
db.commit()
cursor1.execute("show databases")
print()
print("Database: ")
for r in cursor1:
print(r)
cursor1.close()
db.close()

Output:
Conclusion: Hence we have successfully executed the given problem statement.

Lab outcome: Explain how to design GUI Applications in Python and evaluate
different database operations.

********************************************************************

You might also like