PRACTICAL-9: Python Database Connectivity
PRACTICAL-9: Python Database Connectivity
1. Write a python code to establish connection with MySQL and create database demo_data.
Also display list of all the availabledatabase.
Input:
import pymysql
try:
con = pymysql.connect(host="localhost",user="root",passwd="") query = "show
databases" print("available databases are") cursor = con.cursor() cursor.execute(query)
print(type(cursor)) print(cursor)
for i in cursor: print(i)
except pymysql.DatabaseError as e:
print("Your error is : ",e)
finally:
if con:
con.close()
except pymysql.DatabaseError as e:
print("Your error is : ",e)
finally: if cursor:
cursor.close() if con:
con.close()
Output:
2. Write a python code to create below specified two tables ‘hospital_details’ and
‘doctor_details’ and insert values as mentionedbelow.
Input:
import pymysql try: con =
pymysql.connect(host="localhost",user="root",passwd="",database="demo") query =
DIKSHANT | 20012011037 1
CE PYTHON PROGRAMMING AB12
except pymysql.DatabaseError as e:
print("Your error is : ",e)
finally: if cursor:
cursor.close() if con:
con.close()
except pymysql.DatabaseError as e:
print("Your error is : ",e)
finally: if cursor:
cursor.close() if con:
con.close()
except pymysql.DatabaseError as e:
print("Your error is : ",e)
finally: if cursor:
cursor.close() if con:
con.close()
DIKSHANT | 20012011037 2
CE PYTHON PROGRAMMING AB12
[(1,'janta',200),(2,'zydus',500),(3,'sal',1000),(4,'stirling',1500)]
cursor.executemany(query,record) con.commit()
except pymysql.DatabaseError as e:
print("Your error is : ",e)
finally: if cursor:
cursor.close() if con:
con.close()
Output:
except pymysql.DatabaseError as e:
print("Your error is : ",e)
finally: if cursor:
cursor.close() if con:
con.close()
Output:
DIKSHANT | 20012011037 3
CE PYTHON PROGRAMMING AB12
4. Write a python code to retrieve all the doctors who are in Janta hospital.
Input:
import pymysql try: con =
pymysql.connect(host="localhost",user="root",passwd="",database="demo") query =
"select * from doctor_detailsd,hospital_details h where
h.hospital_id=d.hospital_id and h.hodpital_name='janta'" cursor =
con.cursor() cursor.execute(query) x=cursor.fetchall() for i inx:
print(i) con.commit()
except pymysql.DatabaseError as e:
print("Your error is : ",e)
finally:
if cursor:
cursor.close()
if con:
con.close()
Output:
cursor.execute(query)
con.commit()
else:
DIKSHANT | 20012011037 4
CE PYTHON PROGRAMMING AB12
print("notcommited")
except pymysql.DatabaseError as e:
print("Your error is :",e)
finally: if cursor:
cursor.close() if con:
con.close()
Output:
else:
print("notcommited")
except pymysql.DatabaseError as e:
print("Your error is :",e)
finally: if cursor:
cursor.close() if con:
con.close()
Output:
DIKSHANT | 20012011037 5
CE PYTHON PROGRAMMING AB12
7. Write a python code to create in memory database and table using sqlite databaseengine.
Input:
import sqlite3 try: con =
sqlite3.connect("demo_data.db") cursor =
con.cursor() query = "" if con:
cursor.execute(query)
print("Database created")
con.commit() else:
print("Not Established")
except sqlite3.DatabaseError as e:
print("Exception: " , e) finally: if cursor:
cursor.close() if con:
con.close()
Output:
8. Establish connection with sqlite database engine and create above tables into database
demo. Show demonstration of executescript to execute multiple queries at a time.
Input:
import sqlite3 try: con =
sqlite3.connect("demo_data.db") cursor =
con.cursor() query ="" if con:
cursor.executescript( """ DROP TABLE IF EXISTSdoctors_details;
CREATE TABLE doctors_details(doctor_idint,doctor_name
varchar(20),hospital_idint,speciality varchar(30),salary int,experinveint);
INSERT INTO doctors_details VALUES(101,'karan',1,'Pediatric',40000,0); INSERT INTO
doctors_details VALUES(102,'naresh',1,'Onchologist',80000,5); INSERT INTO
doctors_details VALUES(103,'hardil',2,'surgen',60000,2); INSERT INTO doctors_details
VALUES(104,'vishal',2,'Homeopathy',50000,1); INSERT INTO doctors_details
VALUES(105,'jay',3,'Aayurvedic',40000,0); INSERT INTO doctors_details
VALUES(106,'deep',3,'Physeotherapist',70000,4); INSERT INTO doctors_details
VALUES(107,'diveys',4,'Pediatric ',55000,3);""") print("tabel created")
con.commit() else: print("Not Established")
except sqlite3.DatabaseError as e:
print("Exception: " , e) finally: if
cursor: cursor.close() if con:
DIKSHANT | 20012011037 6
CE PYTHON PROGRAMMING AB12
con.close()
Output:
DIKSHANT | 20012011037 7