0% found this document useful (0 votes)
6 views

cs program 16 to 20

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

cs program 16 to 20

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1

16.Write a python program to show mysql


database connectivity and create the table
“Employee”
Program :
import mysql.connector as mysqlcon
mycon=
mysqlcon.connect(host="localhost",user="root",passwd="dbms",database="
school")
if mycon.is_connected()==False:
print("Error connecting to mysql")
cursor = mycon.cursor()
result :
2

17. Write a program to insert the record into


the table employee using python.
Program :
import mysql.connector as mysqlcon
mycon=
mysqlcon.connect(host="localhost",user="root",passwd="dbms",database
="school" )
if mycon.is_connected()==False:
print("Error connecting to mysql")
cursor = mycon.cursor()
# Prepareing SQL statement to insert one record with the given value.
try:
cursor.execute("INSERT INTO EMPLOYEE VALUES (1,'ANIL
KUMAR',86000);")
cursor.execute("INSERT INTO EMPLOYEE VALUES (2,'Naman',82000);")
mycon.commit()
except:
mycon.rollback()
mycon.close()
result :
3

18. Write a program to fetch all records


from employee table having salary more
than 80000 in python.
Program :
import mysql.connector as mysqlcon
mycon=mysqlcon.connect(host="localhost",user="root",passwd="dbms",
database ="school" )
if mycon.is_connected()==False:
print("Error connecting to mysql")
cursor = mycon.cursor()
sql = "SELECT * FROM EMPLOYEE;"
try:
cursor.execute(sql)
#using fetchall() function to fetch all records from the table EMP and store
inresultset
resultset = cursor.fetchall()
for row in resultset:
print (row)
except:
print ("Error: unable to fetch data")
mycon.close()
result :
4

19. Write a program to update the record


salary =salary+1000 where salary >80000.
Program :
import mysql.connector as mysqlcon
mycon=
mysqlcon.connect(host="localhost",user="root",passwd="dbms",database
="school" )
if mycon.is_connected()==False:
print("Error connecting to mysql")
cursor = mycon.cursor()
sql ="update EMPLOYEE SET salary=salary+1000 where salary>80000;"
try:
cursor.execute(sql)
cursor.execute("select *from employee;")
mycon.commit()
except:
mycon.rollback()
mycon.close()
result :
5

20. Write a program to delete the record


from the database.
Program :
import mysql.connector as mysqlcon
mycon=
mysqlcon.connect(host="localhost",user="root",passwd="dbms",datab
ase="school" )
if mycon.is_connected()==False:
print("Error connecting to mysql")
cursor = mycon.cursor()
name=input("Enter name whose record to be deleted : ")
sql = "DELETE FROM EMPLOYEE WHERE NAME=name;"
try:
cursor.execute(sql)
resultset=cursor.execute("select *from employee")
data=cursor.fetchall()
for row in data:
print(data)
print(cursor.rowcount, end=" record(s) deleted ")
mycon.commit()
except:
mycon.rollback()
mycon.close()

result :

You might also like