Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
34 views

NAME-ANIKET.K - I2-39 Experiment No.: 11 C Aim: Program Code

The document describes a Python program that connects to a MySQL database and performs various data manipulation language (DML) operations. These include creating databases and tables, inserting data, updating records, ordering and filtering queries, and dropping databases and tables. The program connects to the local database, creates a 'company' database and 'employee' table, inserts sample employee records, performs select, update, delete and other queries, and finally drops the database and tables.

Uploaded by

aniket kasturi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

NAME-ANIKET.K - I2-39 Experiment No.: 11 C Aim: Program Code

The document describes a Python program that connects to a MySQL database and performs various data manipulation language (DML) operations. These include creating databases and tables, inserting data, updating records, ordering and filtering queries, and dropping databases and tables. The program connects to the local database, creates a 'company' database and 'employee' table, inserts sample employee records, performs select, update, delete and other queries, and finally drops the database and tables.

Uploaded by

aniket kasturi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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