Pymysql 4
Pymysql 4
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 products;')
cur.execute('use products;')
# Create the table
cur.execute('create table if not exists mobile(m_id int(3),Brand varchar(25),model varchar
(50),Year int(4),Price int(7));')
# Insert 15 records into the table
cur.execute('''insert into mobile values
(251, 'Apple', 'iPhone 12', 2020, 79999),
(122, 'Samsung', 'Galaxy S21', 2021, 89999),
(309, 'Google', 'Pixel 4', 2019, 59999),
(443, 'Amazon', 'Echo Dot', 2018, 94999),
(544, 'Microsoft', 'Surface Laptop', 2020, 99999),
(656, 'Huawei', 'Mate 30', 2019, 69999),
(709, 'Xiaomi', 'Redmi Note 9', 2020, 29999),
(862, 'Oppo', 'Find X2', 2020, 49999),
(964, 'Vivo', 'V20', 2020, 39999),
(510, 'OnePlus', '8 Pro', 2020, 69999),
(161, 'LG', 'G8X', 2019, 59999),
(412, 'Sony', 'Xperia 1', 2019, 79999),
(134, 'Motorola', 'Razr', 2020, 149999),
(184, 'Nokia', '8.3', 2020, 49999),
(915, 'Asus', 'Zenfone 7', 2020, 59999);''')
#Functions for 'transport' table
def add_mobile():
import random
x=random.randint(100,999)
print("New Mobile id is ",x)
m_id=x
brand=input("Enter brand : ")
model=input("Enter model of mobile :")
year=int(input("Enter year of launching : "))
price=int(input("Enter price of mobile : "))
sol="insert into mobile values('%d','%s','%s','%d','%d')"%(m_id,brand,model,year,price)
cur.execute(sol)
conn.commit()
print('updated successfully')
g=cur.execute("select * from mobile;")
h=cur.fetchall()
for i in h:
print(i)
def update_brand():
m_id=int(input("Enter mobile Id :"))
brand=input("Enter updated brand : ")
sol="update mobile set brand='%s' where m_id='%d' "%(brand,m_id)
cur.execute(sol)
conn.commit()
print('updated successfully')
query="select * from mobile where m_id='%d' "%(m_id)
cur.execute(query)
h=cur.fetchall()
for i in h:
print(i)
def update_model():
m_id=int(input("Enter mobile Id :"))
model=input("Enter updated model : ")
sol="update mobile set model='%s' where m_id='%d' "%(model,m_id)
cur.execute(sol)
conn.commit()
print('updated successfully')
query="select * from mobile where m_id='%d' "%(m_id)
cur.execute(query)
h=cur.fetchall()
for i in h:
print(i)
def update_year():
m_id=int(input("Enter mobile Id :"))
year=int(input("Enter updated year of launching : "))
sol="update mobile set year='%d' where m_id='%d' "%(year,m_id)
cur.execute(sol)
conn.commit()
print('updated successfully')
query="select * from mobile where m_id='%d' "%(m_id)
cur.execute(query)
h=cur.fetchall()
for i in h:
print(i)
def update_price():
m_id=int(input("Enter mobile Id :"))
price=int(input("Enter updated price : "))
sol="update mobile set price='%d' where m_id='%d' "%(price,m_id)
cur.execute(sol)
conn.commit()
print('updated successfully')
query="select * from mobile where m_id='%d' "%(m_id)
cur.execute(query)
h=cur.fetchall()
for i in h:
print(i)
def delete_mobile():
m_id=int(input("Enter mobile id of mobile to be abandoned :"))
sol="delete from mobile where m_id='%d' "%(m_id)
cur.execute(sol)
conn.commit()
print(' Abandoned successfully')
def search_mobile():
m_id=int(input("Enter mobile id to search :"))
sol="select * from mobile where m_id='%d' "%(m_id)
cur.execute(sol)
h=cur.fetchall()
k=0
for i in h:
k=1
if k==1:
print("Yes this mobile id exists")
print(i)
if k==0:
print("No this mobile id doesn't exist")
#FUNCTIONS DEFINED TO UPDATE DATA OF MOBILES
def update_data():
while True:
print("What do you want to update sir")
print("1.Update brand name")
print("2.Update model ")
print("3.Update year of launch")
print("4.Update price")
print("5.Get Back")
z=int(input("May I know your need SIR : "))
if z==1:
update_brand()
elif z==2:
update_model()
elif z==3:
update_year()
elif z==4:
update_price()
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 [MOBILE]")
while True:
print("Kindly provide us your reason to visit by telling the serial number of following things ")
print("1.View the data of mobiles")
print("2.Add the data of mobile")
print("3.Update the data of mobiles")
print("4.Search the data of mobiles")
print("5.Delete a data of mobiles")
print("6.Exit")
y=int(input("Enter serial number : "))
if y==1:
g=cur.execute("select * from mobile;")
h=cur.fetchall()
for i in h:
print(i)
elif y==2:
add_mobile()
elif y==3:
update_data()
elif y==4:
search_mobile()
elif y==5:
delete_mobile()
elif y==6:
print("Thank you for coming")
break
else:
print("Invalid choice")
continue
OUTPUT
Enter your name : sukhwinder
Welcome sukhwinder to our program [MOBILE]
Kindly provide us your reason to visit by telling the serial number of following things
1.View the data of mobiles
2.Add the data of mobile
3.Update the data of mobiles
4.Search the data of mobiles
5.Delete a data of mobiles
6.Exit
Enter serial number : 1
(251, 'Apple', 'iPhone 12', 2020, 79999)
(122, 'Samsung', 'Galaxy S21', 2021, 89999)
(309, 'Google', 'Pixel 4', 2019, 59999)
(443, 'Amazon', 'Echo Dot', 2018, 94999)
(544, 'Microsoft', 'Surface Laptop', 2020, 99999)
(656, 'Huawei', 'Mate 30', 2019, 69999)
(709, 'Xiaomi', 'Redmi Note 9', 2020, 29999)
(862, 'Oppo', 'Find X2', 2020, 49999)
(964, 'Vivo', 'V20', 2020, 39999)
(510, 'OnePlus', '8 Pro', 2020, 69999)
(161, 'LG', 'G8X', 2019, 59999)
(412, 'Sony', 'Xperia 1', 2019, 79999)
(134, 'Motorola', 'Razr', 2020, 149999)
(184, 'Nokia', '8.3', 2020, 49999)
(915, 'Asus', 'Zenfone 7', 2020, 59999)
Kindly provide us your reason to visit by telling the serial number of following things
1.View the data of mobiles
2.Add the data of mobile
3.Update the data of mobiles
4.Search the data of mobiles
5.Delete a data of mobiles
6.Exit
Enter serial number : 2
New Mobile id is 277
Enter brand : Google
Enter model of mobile :Pixel 5
Enter year of launching : 2019
Enter price of mobile : 69999
updated successfully
(251, 'Apple', 'iPhone 12', 2020, 79999)
(122, 'Samsung', 'Galaxy S21', 2021, 89999)
(309, 'Google', 'Pixel 4', 2019, 59999)
(443, 'Amazon', 'Echo Dot', 2018, 94999)
(544, 'Microsoft', 'Surface Laptop', 2020, 99999)
(656, 'Huawei', 'Mate 30', 2019, 69999)
(709, 'Xiaomi', 'Redmi Note 9', 2020, 29999)
(862, 'Oppo', 'Find X2', 2020, 49999)
(964, 'Vivo', 'V20', 2020, 39999)
(510, 'OnePlus', '8 Pro', 2020, 69999)
(161, 'LG', 'G8X', 2019, 59999)
(412, 'Sony', 'Xperia 1', 2019, 79999)
(134, 'Motorola', 'Razr', 2020, 149999)
(184, 'Nokia', '8.3', 2020, 49999)
(915, 'Asus', 'Zenfone 7', 2020, 59999)
(277, 'Google', 'Pixel 5', 2019, 69999)
Kindly provide us your reason to visit by telling the serial number of following things
1.View the data of mobiles
2.Add the data of mobile
3.Update the data of mobiles
4.Search the data of mobiles
5.Delete a data of mobiles
6.Exit
Enter serial number : 3
What do you want to update sir
1.Update brand name
2.Update model
3.Update year of launch
4.Update price
5.Get Back
May I know your need SIR : 1
Enter mobile Id :277
Enter updated brand : Vivo
updated successfully
(277, 'Vivo', 'Pixel 5', 2019, 69999)
What do you want to update sir
1.Update brand name
2.Update model
3.Update year of launch
4.Update price
5.Get Back
May I know your need SIR : 2
Enter mobile Id :277
Enter updated model : V21
updated successfully
(277, 'Vivo', 'V21', 2019, 69999)
What do you want to update sir
1.Update brand name
2.Update model
3.Update year of launch
4.Update price
5.Get Back
May I know your need SIR : 3
Enter mobile Id :277
Enter updated year of launching : 2020
updated successfully
(277, 'Vivo', 'V21', 2020, 69999)
What do you want to update sir
1.Update brand name
2.Update model
3.Update year of launch
4.Update price
5.Get Back
May I know your need SIR : 4
Enter mobile Id :277
Enter updated price : 59999
updated successfully
(277, 'Vivo', 'V21', 2020, 59999)
What do you want to update sir
1.Update brand name
2.Update model
3.Update year of launch
4.Update price
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 mobiles
2.Add the data of mobile
3.Update the data of mobiles
4.Search the data of mobiles
5.Delete a data of mobiles
6.Exit
Enter serial number : 4
Enter mobile id to search :277
Yes this mobile id exists
(277, 'Vivo', 'V21', 2020, 59999)
Kindly provide us your reason to visit by telling the serial number of following things
1.View the data of mobiles
2.Add the data of mobile
3.Update the data of mobiles
4.Search the data of mobiles
5.Delete a data of mobiles
6.Exit
Enter serial number : 5
Enter mobile id of mobile to be abandoned :277
Abandoned successfully
Kindly provide us your reason to visit by telling the serial number of following things
1.View the data of mobiles
2.Add the data of mobile
3.Update the data of mobiles
4.Search the data of mobiles
5.Delete a data of mobiles
6.Exit
Enter serial number : 6
Thank you for coming