-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_helper.py
126 lines (87 loc) · 2.66 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import mysql.connector
# Connect to the database
cnx = mysql.connector.connect(
host="localhost",
user="root",
password="root",
database="pandeyji_eatery"
)
def insert_order_item(food_item: str, quantity: int, order_id: int):
try:
# Create a cursor object
cursor = cnx.cursor()
# Calling the stored procedure
cursor.callproc("insert_order_item", (food_item, quantity, order_id))
# Commit the changes
cnx.commit()
# Close the cursor and the connection
cursor.close()
# cnx.close()
print("Order item inserted successfully")
return 1
except Exception as e:
print(f"An error occurred: {e}")
# Rollback the changes if necessary
cnx.rollback()
return -1
def get_total_order_price(order_id: int):
# Create a cursor object
cursor = cnx.cursor()
# Write the SQL query
query = f"SELECT get_total_order_price({order_id})"
# Execute the query
cursor.execute(query)
# Fetch the result
result = cursor.fetchone()[0]
# Close the cursor and the connection
cursor.close()
# cnx.close()
return result
def insert_order_tracking(order_id: int, status: str):
try:
# Create a cursor object
cursor = cnx.cursor()
# Calling the stored procedure
insert_query = (
"INSERT INTO order_tracking (order_id, status) VALUES (%s, %s)")
cursor.execute(insert_query, (order_id, status))
# Commit the changes
cnx.commit()
# Close the cursor and the connection
cursor.close()
# cnx.close()
except Exception as e:
print(f"An error occurred: {e}")
# Rollback the changes if necessary
cnx.rollback()
return -1
def get_order_status(order_id: int):
# Create a cursor object
cursor = cnx.cursor()
# Write the SQL query
query = ("SELECT status FROM order_tracking WHERE order_id = %s")
# Execute the query
cursor.execute(query, (order_id,))
# Fetch the result
result = cursor.fetchone()
# Close the cursor and the connection
cursor.close()
# cnx.close()
if result is not None:
return result[0]
else:
return None
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 order_tracking")
cursor.execute(query)
# Fetching the result
result = cursor.fetchone()[0]
# Close the cursor
cursor.close()
# Returning the next available order_id
if result is None:
return 1
else:
return result + 1