-
Notifications
You must be signed in to change notification settings - Fork 1
/
code
62 lines (56 loc) · 1.59 KB
/
code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import mysql.connector
choice=int(input('''How do you want to proceed?:
1.Insert values
2.Modify values
3.Delete values
4.display values'''))
mydb = mysql.connector.connect(
host='localhost',
user="root",
password="root",
database="mydb"
)
cursor = mydb.cursor()
qu='use mydb'
cursor.execute(qu)
qur='''create table if not exists(
product_name primary key
description
quantity
price
)'''
cursor.execute(qur)
def add_product(name, description, quantity, price):
sql = "INSERT INTO products (product_name, description, quantity, price) VALUES (%s, %s, %s, %s)"
val = (name, description, quantity, price)
cursor.execute(sql, val)
mydb.commit()
print("Product added successfully!")
def delete_product(product_id):
sql = "DELETE FROM products WHERE product_id = %s"
val = (product_id,)
cursor.execute(sql, val)
mydb.commit()
print("Product deleted successfully!")
def modify_product(product_id, new_name, new_description, new_quantity, new_price):
sql = "UPDATE products SET product_name = %s, description = %s, quantity = %s, price = %s WHERE product_id = %s"
val = (new_name, new_description, new_quantity, new_price, product_id)
cursor.execute(sql, val)
mydb.commit()
print("Product modified successfully!")
def get_all_products():
cursor.execute("SELECT * FROM products")
result = cursor.fetchall()
for row in result:
print(row)
if choice==1:
add_product()
elif choice==2:
modify_product()
elif choice==3:
delete_product()
elif choice==4:
get_all_products()
else:
print('wrong input')
mydb.close()