0% found this document useful (0 votes)
8 views2 pages

Python MySQL Connectivity Programmes

The document provides Python programs to connect to a MySQL database named 'hospital' for various operations. It includes code snippets for displaying patient details, inserting new patient records, updating a patient's department, and querying patients based on user-specified age and department. Each program establishes a connection to the database and executes the relevant SQL commands.

Uploaded by

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

Python MySQL Connectivity Programmes

The document provides Python programs to connect to a MySQL database named 'hospital' for various operations. It includes code snippets for displaying patient details, inserting new patient records, updating a patient's department, and querying patients based on user-specified age and department. Each program establishes a connection to the database and executes the relevant SQL commands.

Uploaded by

pandaanupam833
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Write a programme in Python based on the following database and table to connect to
MySQL and display details of patients.

Database name : ******* ( your database name )

Table name : hospital

import [Link] as p
q=[Link](host=’localhost’,user=’root’,passwd=’tiger’,database=’**********’)
if q.is_connected()==True:
print(‘Connection successful…..’)
else:
print(‘Connection unsuccessful…..’)
r=[Link]()
[Link](“select * from hospital”)
data=[Link]()
for i in data:
print(i)

2. Write a programme in Python based on the following database and table to connect to
MySQL and insert details of patients.

Database name : ******* ( your database name )

Table name : hospital

import [Link] as p
q=[Link](host=’localhost’,user=’root’,passwd=’tiger’,database=’**********’)
if q.is_connected()==True:
print(‘Connection successful…..’)
else:
print(‘Connection unsuccessful…..’)
r=[Link]()
s=”insert into hospital values({},’{}’,’{}’,’{}’,{}”.format(321,’Jonathan
Pat’,’ENT’,’Male’,32)
[Link](s)
[Link]()
3. 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 : ******* ( your database name )

Table name : hospital

import [Link] as p
q=[Link](host=’localhost’,user=’root’,passwd=’tiger’,database=’**********’)
if q.is_connected()==True:
print(‘Connection successful…..’)
else:
print(‘Connection unsuccessful…..’)
r=[Link]()
s=”update hospital set dept=’{}’ where pid={}”.format(‘cardio’,’neuro’,326)
[Link](s)
[Link]()

4. 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 is given by the user
.

Database name : ******* ( your database name )

Table name : hospital

import [Link] as p
q=[Link](host=’localhost’,user=’root’,passwd=’tiger’,database=’**********’)
if q.is_connected()==True:
print(‘Connection successful…..’)
else:
print(‘Connection unsuccessful…..’)
r=[Link]()
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)


data=[Link](s)
for i in data:
print(i)

You might also like