0% found this document useful (0 votes)
17 views

SQL Record

The document contains Python code to connect to a MySQL database called 'cvt2' and perform CRUD operations on a table called 'hosp'. It updates charges by 20%, selects data for males, creates the table and inserts sample records, and deletes a record based on user input of sno.

Uploaded by

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

SQL Record

The document contains Python code to connect to a MySQL database called 'cvt2' and perform CRUD operations on a table called 'hosp'. It updates charges by 20%, selects data for males, creates the table and inserts sample records, and deletes a record based on user input of sno.

Uploaded by

Sakshi Mehta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

S.

Krish Hariharan 12114


CODE:
import mysql.connector as sql
connection=sql.connect(host='localhost',user='root',password='admin',database='cvt2')
cursor=connection.cursor()
query="update hosp set charges=charges+(0.2*charges)"
cursor.execute(query)
cursor.execute("select*from hosp;")
data=cursor.fetchall()
for i in data:
print(i)
connection.close()

OUTPUT:
(1, 'arpit', 52, 'surgery', datetime.date(2015, 5, 21), 360, 'm')
(2, 'bala', 49, 'ortho', datetime.date(2015, 5, 23), 300, 'm')
(3, 'varsha', 40, 'dentist', datetime.date(2014, 4, 21), 360, 'f')
(4, 'abhi', 45, 'cardio', datetime.date(2013, 7, 27), 360, 'm')
(5, 'sunitha', 49, 'pediatrition', datetime.date(2015, 9, 21), 360, 'f')

S. Krish Hariharan
CODE:
import mysql.connector as sql
connection=sql.connect(host='localhost',user='root',password='admin',database='cvt2')
cursor=connection.cursor()
query="select*from hosp where sex = '%s'"%('m')
cursor.execute(query)
data=cursor.fetchall()
for row in data:
print(row)
connection.close()

OUTPUT:
(1, 'arpit', 52, 'surgery', datetime.date(2015, 5, 21), 300, 'm')
(2, 'bala', 49, 'ortho', datetime.date(2015, 5, 23), 250, 'm')
(4, 'abhi', 45, 'cardio', datetime.date(2013, 7, 27), 300, 'm')
S.Krish Hariharan 12114
CODE:
import mysql.connector as sql
connection=sql.connect(host='localhost',user='root',password='admin',database='cvt2')
cursor=connection.cursor()
query=('create table hosp (sno int, name varchar(15), age int, department varchar(20),
DOA date, charges int, sex char(1))')
cursor.execute(query)
cursor.execute("insert into hosp value (1,'arpit',52,'surgery',20150521,300,'m')")
cursor.execute("insert into hosp value (2,'bala',49,'ortho',20150523,250,'m')")
cursor.execute("insert into hosp value (3,'varsha',40,'dentist',20140421,300,'f')")
cursor.execute("insert into hosp value (4,'abhi',45,'cardio',20130727,300,'m')")
cursor.execute("insert into hosp value (5,'sunitha',49,'pediatrition',20150921,300,'f')")
connection.commit()
cursor.execute("select*from hosp")
lst=cursor.fetchall()
for row in lst:
print(row)
connection.close()

OUTPUT:
(1, 'arpit', 52, 'surgery', datetime.date(2015, 5, 21), 300, 'm')
(2, 'bala', 49, 'ortho', datetime.date(2015, 5, 23), 250, 'm')
(3, 'varsha', 40, 'dentist', datetime.date(2014, 4, 21), 300, 'f')
(4, 'abhi', 45, 'cardio', datetime.date(2013, 7, 27), 300, 'm')
(5, 'sunitha', 49, 'pediatrition', datetime.date(2015, 9, 21), 300, 'f')

S.Krish Hariharan 12114


CODE:
import mysql.connector as sql
connection=sql.connect(host='localhost',user='root',password='admin',database='cvt2')
cursor=connection.cursor()
sn=int(input("Enter the pid to be deleted:"))
query=f"""delete from hospi where sno='{sn}';"""
cursor.execute(query)
connection.commit()
cursor.execute("select*from hospi;")
data=cursor.fetchall()
for i in data:
print(i)
connection.close()
OUTPUT:
Enter the pid to be deleted:1
(2, 'bala', 49, 'ortho', datetime.date(2015, 5, 23), 250, 'm')
(3, 'varsha', 40, 'dentist', datetime.date(2014, 4, 21), 300, 'f')
(4, 'abhi', 45, 'cardio', datetime.date(2013, 7, 27), 300, 'm')
(5, 'sunitha', 49, 'pediatrition', datetime.date(2015, 9, 21), 300, 'f')

You might also like