Python-MySQL-stub
Python-MySQL-stub
Write a programme in Python based on the following database and table to connect to
MySQL and display details of patients.
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.
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 .
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.
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 :’))