Program:11
#11
#where condition
#searching a record by name
#select command
import mysql.connector
from mysql.connector import Error
con=None
try:
con = mysql.connector.connect(host='localhost',user='root',
password='ct',database='student1')
if(con.is_connected()==True):
print("connected")
db=con.cursor()
sn=input("Enter name of the student ")
sql=("select * from stud where name='{}'".format(sn))
db.execute(sql)
res = db.fetchall()
for x in res:
print(x)
except Error as e:
print(e)
finally:
if con is not None and con.is_connected():
con.close()
print("Connection closed")
Program:12
#12
#delete a record
#delete by name
#to display all records from a table
#select command
import mysql.connector
from mysql.connector import Error
con=None
try:
con = mysql.connector.connect(host='localhost',user='root',
password='ct',database='student1')
if(con.is_connected()==True):
print("connected")
db=con.cursor()
sn=input("Enter name ")
sql="select * from stud where name='{}'".format(sn)
db.execute(sql)
res = db.fetchall()
print("records to be deleted are")
for x in res:
print(x)
sql="delete from stud where name='{}'".format(sn)
db.execute(sql)
print(db.rowcount, "record deleted.")
con.commit();
except Error as e:
print(e)
finally:
if con is not None and con.is_connected():
con.close()
print("Connection closed")
Program:13
#13
#update all records
#to display all records from a table
#select command
import mysql.connector
from mysql.connector import Error
con=None
try:
con = mysql.connector.connect(host='localhost',user='root',
password='ct',database='student1')
if(con.is_connected()==True):
print("connected")
db=con.cursor()
sql="update stud set total=m1+m2+m3+m4+m5"
db.execute(sql)
sql="select * from stud"
db.execute(sql)
res = db.fetchall()
for x in res:
print(x)
con.commit();
except Error as e:
print(e)
finally:
if con is not None and con.is_connected():
con.close()
print("Connection closed")




