-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_helper.py
108 lines (77 loc) · 2.53 KB
/
db_helper.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import mysql.connector
cnx = mysql.connector.connect(
"Enter the credentails to connect database"
)
def insert_order_item(food_item, quantity, order_id):
try:
cursor = cnx.cursor()
# Calling the stored procedure
cursor.callproc('insert_order_item', (food_item, quantity, order_id))
# Committing the changes
cnx.commit()
# Closing the cursor
cursor.close()
print("Order item inserted successfully!")
return 1
except mysql.connector.Error as err:
print(f"Error inserting order item: {err}")
# Rollback changes if necessary
cnx.rollback()
return -1
except Exception as e:
print(f"An error occurred: {e}")
# Rollback changes if necessary
cnx.rollback()
return -1
# Function to insert a record into the order_tracking table
def insert_order_tracking(order_id, status):
cursor = cnx.cursor()
# Inserting the record into the order_tracking table
insert_query = "INSERT INTO order_tracking (order_id, status) VALUES (%s, %s)"
cursor.execute(insert_query, (order_id, status))
# Committing the changes
cnx.commit()
# Closing the cursor
cursor.close()
def get_total_order_price(order_id):
cursor = cnx.cursor()
# Executing the SQL query to get the total order price
query = f"SELECT get_total_order_price({order_id})"
cursor.execute(query)
# Fetching the result
result = cursor.fetchone()[0]
# Closing the cursor
cursor.close()
return result
# Function to get the next available order_id
def get_next_order_id():
cursor = cnx.cursor()
# Executing the SQL query to get the next available order_id
query = "SELECT MAX(order_id) FROM orders"
cursor.execute(query)
# Fetching the result
result = cursor.fetchone()[0]
# Closing the cursor
cursor.close()
# Returning the next available order_id
if result is None:
return 1
else:
return result + 1
# Function to fetch the order status from the order_tracking table
def get_order_status(order_id):
cursor = cnx.cursor()
# Executing the SQL query to fetch the order status
query = f"SELECT status FROM order_tracking WHERE order_id = {order_id}"
cursor.execute(query)
# Fetching the result
result = cursor.fetchone()
# Closing the cursor
cursor.close()
# Returning the order status
if result:
return result[0]
else:
return None
if __name__=="__main__":
insert_order_item('cheese pizza',20,50)