Q1.
Write a programme in Python based on the following database and table to connect to
MySQL and display details of patients.
Database name : XYZ
Table name : hospital
import mysql.connector as p
q=p.connect(host=’localhost’,user=’root’,passwd=’tiger’,database=’XYZ’)
if q.is_connected( ) = = True:
print(‘Connection successful…..’)
else:
print(‘Connection unsuccessful…..’)
r=q.cursor()
r.execute(“select * from hospital”)
data=r.fetchall( )
for i in data:
print(i)
q.close( )
Q2. Write a programme in Python based on the following database and table to connect to
MySQL and insert details of patients.
Database name : XYZ
Table name : hospital
import mysql.connector as p
q=p.connect(host=’localhost’,user=’root’,passwd=’tiger’,database=’ XYZ ’)
if q.is_connected( ) = = True:
print(‘Connection successful…..’)
else:
print(‘Connection unsuccessful…..’)
r=q.cursor( )
s=”insert into hospital values({},’{}’,’{}’,’{}’,{})”.format(321,’Jonathan
Pat’,’ENT’,’Male’,32)
r.execute(s)
q.commit( )
q.close( )
Q3.Write a programme in Python based on the following database and table to connect to
MySQL and change department from cardio to neuro whose patient id is 326 .
Database name : XYZ
Table name : hospital
import mysql.connector as p
q=p.connect(host=’localhost’,user=’root’,passwd=’tiger’,database=’XYZ’)
if q.is_connected( )==True:
print(‘Connection successful…..’)
else:
print(‘Connection unsuccessful…..’)
r=q.cursor( )
s=”update hospital set dept=’{}’ where dept=’{}’and pid={}”.format(’neuro’, ‘cardio’,326)
r.execute(s)
q.commit( )
q.close( )
Q4. Write a programme in Python based on the following database and table to connect to
MySQL and display details of patients whose age and department are given by the user.
Database name : XYZ
Table name : hospital
import mysql.connector as p
q=p.connect(host=’localhost’,user=’root’,passwd=’tiger’,database=’XYZ’)
if q.is_connected( )==True:
print(‘Connection successful…..’)
else:
print(‘Connection unsuccessful…..’)
r=q.cursor( )
x=int(input(‘Enter age of the patient :’))
y=input(‘Enter department of the patient :’))
s=”select * from hospital where age={} and dept=’{}’ ”.format(x,y)
r.execute(s)
data=r.fetchall( )
for i in data:
print(i)
q.close( )