0% found this document useful (0 votes)
15 views12 pages

Document 4

Uploaded by

aaditya160807
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)
15 views12 pages

Document 4

Uploaded by

aaditya160807
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
You are on page 1/ 12

PYMYSQL-2

import pymysql
# Connect to the database
conn = pymysql.connect(host='localhost',user='root',password='aaditya2007',)
# Create a cursor object
cur = conn.cursor()
#creating database
cur.execute('create database if not exists transport;')
cur.execute('use transport;')
#creating Table-1
cur.execute('create table if not exists transport(s_id int(3),Car_Model varchar(200),Type varchar
(50),Year int(4),Horse_power int(3));')
# Insert 15 records into the table
cur.execute('''insert into transport values
(758, 'Toyota Camry', 'Sedan', 2020, 203),
(987, 'Honda Civic', 'Sedan', 2019, 180),
(364, 'Ford Mustang', 'Sports', 2018, 310),
(814, 'Nissan Altima', 'Sedan', 2017, 188),
(575, 'Chevrolet Cruze', 'Sedan', 2016, 153),
(369, 'Hyundai Elantra', 'Sedan', 2015, 145),
(287, 'Mazda3', 'Sedan', 2014, 184),
(558, 'Subaru Outback', 'Wagon', 2013, 260),
(299, 'Volkswagen Jetta', 'Sedan', 2012, 170),
(140, 'Kia Optima', 'Sedan', 2011, 200),
(941, 'BMW 3 Series', 'Luxury', 2010, 300),
(528, 'Mercedes-Benz C-Class', 'Luxury', 2009, 228),
(282, 'Audi A4', 'Luxury', 2008, 211),
(644, 'Lexus IS', 'Luxury', 2007, 306),
(857, 'Porsche 911', 'Sports', 2006, 355);''')
#Functions for 'transport' table
def add_car():
import random
x=random.randint(100,999)
print("New Car id is ",x)
s_id=x
car=input("Enter car model : ")
type_car=input("Enter type of car :")
year=int(input("Enter year of manufacture : "))
hp=int(input("Enter horse power of car : "))
sol="insert into transport values('%d','%s','%s','%d','%d')"%(s_id,car,type_car,year,hp)
cur.execute(sol)
conn.commit()
print('updated successfully')
g=cur.execute("select * from transport;")
h=cur.fetchall()
for i in h:
print(i)
def update_carmod():
s_id=int(input("Enter serial Id :"))
car=input("Enter updated car model : ")
sol="update transport set car_model='%s' where s_id='%d' "%(car,s_id)
cur.execute(sol)
conn.commit()
print('updated successfully')
query="select * from transport where s_id='%d' "%(s_id)
cur.execute(query)
h=cur.fetchall()
for i in h:
print(i)
def update_cartype():
s_id=int(input("Enter serial Id :"))
type_car=input("Enter updated car type : ")
sol="update transport set type='%s' where s_id='%d' "%(type_car,s_id)
cur.execute(sol)
conn.commit()
print('updated successfully')
query="select * from transport where s_id='%d' "%(s_id)
cur.execute(query)
h=cur.fetchall()
for i in h:
print(i)
def update_year():
s_id=int(input("Enter serial Id :"))
year=int(input("Enter updated year of manufacture : "))
sol="update transport set year='%d' where s_id='%d' "%(year,s_id)
cur.execute(sol)
conn.commit()
print('updated successfully')
query="select * from transport where s_id='%d' "%(s_id)
cur.execute(query)
h=cur.fetchall()
for i in h:
print(i)
def update_hp():
s_id=int(input("Enter serial Id :"))
hp=int(input("Enter updated horsepower : "))
sol="update transport set horse_power='%d' where s_id='%d' "%(hp,s_id)
cur.execute(sol)
conn.commit()
print('updated successfully')
query="select * from transport where s_id='%d' "%(s_id)
cur.execute(query)
h=cur.fetchall()
for i in h:
print(i)
def delete_car():
s_id=int(input("Enter serial id of car to be abandoned :"))
sol="delete from transport where s_id='%d' "%(s_id)
cur.execute(sol)
conn.commit()
print(' Abandoned successfully')
def search_car():
s_id=int(input("Enter car serial id to search :"))
sol="select * from transport where s_id='%d' "%(s_id)
cur.execute(sol)
h=cur.fetchall()
k=0
for i in h:
k=1
if k==1:
print("Yes this car id exists")
print(i)
if k==0:
print("No this car id doesn't exist")
#FUNCTIONS DEFINED TO UPDATE DATA OF UNIVERSITY
def update_data():
while True:
print("What do you want to update sir")
print("1.Update car model")
print("2.Update car type ")
print("3.Update year of manufacturing")
print("4.Update horse power of car")
print("5.Get Back")
z=int(input("May I know your need SIR : "))
if z==1:
update_carmod()
elif z==2:
update_cartype()
elif z==3:
update_year()
elif z==4:
update_hp()
elif z==5 :
print("Thank you coming sir")
break
else :
print("invalid choice sir , please try again")
continue
#USER INTERFACE
a=input("Enter your name : ")
print("Welcome",a,"to our program [CARS]")
while True:
print("Kindly provide us your reason to visit by telling the serial number of following things ")
print("1.View the data of cars")
print("2.Add the data of cars")
print("3.Update the data of cars")
print("4.Search the data of cars")
print("5.Delete a data of cars")
print("6.Exit")
y=int(input("Enter serial number : "))
if y==1:
g=cur.execute("select * from transport;")
h=cur.fetchall()
for i in h:
print(i)
elif y==2:
add_car()
elif y==3:
update_data()
elif y==4:
search_car()
elif y==5:
delete_car()
elif y==6:
print("Thank you for coming")
break
else:
print("Invalid choice")
Continue

OUTPUT
Enter your name : sumit
Welcome sumit to our program [CARS]
Kindly provide us your reason to visit by telling the serial number of following things
1.View the data of cars
2.Add the data of cars
3.Update the data of cars
4.Search the data of cars
5.Delete a data of cars
6.Exit
Enter serial number : 1
(758, 'Toyota Camry', 'Sedan', 2020, 203)
(987, 'Honda Civic', 'Sedan', 2019, 180)
(364, 'Ford Mustang', 'Sports', 2018, 310)
(814, 'Nissan Altima', 'Sedan', 2017, 188)
(575, 'Chevrolet Cruze', 'Sedan', 2016, 153)
(369, 'Hyundai Elantra', 'Sedan', 2015, 145)
(287, 'Mazda3', 'Sedan', 2014, 184)
(558, 'Subaru Outback', 'Wagon', 2013, 260)
(299, 'Volkswagen Jetta', 'Sedan', 2012, 170)
(140, 'Kia Optima', 'Sedan', 2011, 200)
(941, 'BMW 3 Series', 'Luxury', 2010, 300)
(528, 'Mercedes-Benz C-Class', 'Luxury', 2009, 228)
(282, 'Audi A4', 'Luxury', 2008, 211)
(644, 'Lexus IS', 'Luxury', 2007, 306)
(857, 'Porsche 911', 'Sports', 2006, 355)
Kindly provide us your reason to visit by telling the serial number of following things
1.View the data of cars
2.Add the data of cars
3.Update the data of cars
4.Search the data of cars
5.Delete a data of cars
6.Exit
Enter serial number : 2
New Car id is 458
Enter car model : Hyundai Verna
Enter type of car :Sedan
Enter year of manufacture : 2019
Enter horse power of car : 234
updated successfully
(758, 'Toyota Camry', 'Sedan', 2020, 203)
(987, 'Honda Civic', 'Sedan', 2019, 180)
(364, 'Ford Mustang', 'Sports', 2018, 310)
(814, 'Nissan Altima', 'Sedan', 2017, 188)
(575, 'Chevrolet Cruze', 'Sedan', 2016, 153)
(369, 'Hyundai Elantra', 'Sedan', 2015, 145)
(287, 'Mazda3', 'Sedan', 2014, 184)
(558, 'Subaru Outback', 'Wagon', 2013, 260)
(299, 'Volkswagen Jetta', 'Sedan', 2012, 170)
(140, 'Kia Optima', 'Sedan', 2011, 200)
(941, 'BMW 3 Series', 'Luxury', 2010, 300)
(528, 'Mercedes-Benz C-Class', 'Luxury', 2009, 228)
(282, 'Audi A4', 'Luxury', 2008, 211)
(644, 'Lexus IS', 'Luxury', 2007, 306)
(857, 'Porsche 911', 'Sports', 2006, 355)
(458, 'Hyundai Verna', 'Sedan', 2019, 234)
Kindly provide us your reason to visit by telling the serial number of following things
1.View the data of cars
2.Add the data of cars
3.Update the data of cars
4.Search the data of cars
5.Delete a data of cars
6.Exit
Enter serial number : 3
What do you want to update sir
1.Update car model
2.Update car type
3.Update year of manufacturing
4.Update horse power of car
5.Get Back
May I know your need SIR : 1
Enter serial Id :458
Enter updated car model : Verna
updated successfully
(458, 'Verna', 'Sedan', 2019, 234)
What do you want to update sir
1.Update car model
2.Update car type
3.Update year of manufacturing
4.Update horse power of car
5.Get Back
May I know your need SIR : 2
Enter serial Id :458
Enter updated car type : Luxury
updated successfully
(458, 'Verna', 'Luxury', 2019, 234)
What do you want to update sir
1.Update car model
2.Update car type
3.Update year of manufacturing
4.Update horse power of car
5.Get Back
May I know your need SIR : 3
Enter serial Id :458
Enter updated year of manufacture : 2018
updated successfully
(458, 'Verna', 'Luxury', 2018, 234)
What do you want to update sir
1.Update car model
2.Update car type
3.Update year of manufacturing
4.Update horse power of car
5.Get Back
May I know your need SIR : 4
Enter serial Id :458
Enter updated horsepower : 112
updated successfully
(458, 'Verna', 'Luxury', 2018, 112)
What do you want to update sir
1.Update car model
2.Update car type
3.Update year of manufacturing
4.Update horse power of car
5.Get Back
May I know your need SIR : 5
Thank you coming sir
Kindly provide us your reason to visit by telling the serial number of following things
1.View the data of cars
2.Add the data of cars
3.Update the data of cars
4.Search the data of cars
5.Delete a data of cars
6.Exit
Enter serial number : 4
Enter car serial id to search :458
Yes this car id exists
(458, 'Verna', 'Luxury', 2018, 112)
Kindly provide us your reason to visit by telling the serial number of following things
1.View the data of cars
2.Add the data of cars
3.Update the data of cars
4.Search the data of cars
5.Delete a data of cars
6.Exit
Enter serial number : 5
Enter serial id of car to be abandoned :458
Abandoned successfully
Kindly provide us your reason to visit by telling the serial number of following things
1.View the data of cars
2.Add the data of cars
3.Update the data of cars
4.Search the data of cars
5.Delete a data of cars
6.Exit
Enter serial number : 6
Thank you for coming

You might also like