-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsql.py
More file actions
1819 lines (1375 loc) · 67.1 KB
/
sql.py
File metadata and controls
1819 lines (1375 loc) · 67.1 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import mysql.connector
import threading
from flask import Flask, request, jsonify
mydb = mysql.connector.connect(
host="Nikets-MacBook-Air.local",
user="root",
password="Niket@mac",
database="bookshop",
auth_plugin='mysql_native_password'
)
cursor = mydb.cursor()
def change_customer_password(cust_number):
print("Enter OTP")
print("OTP Entered correctly.....")
print()
new_password = input("Enter new password: ")
confirm_password = input("Confirm new password: ")
if new_password != confirm_password:
print("Passwords do not match. Please try again.")
return
try:
cursor.execute("START TRANSACTION")
cursor.execute("UPDATE Customer SET customer_password = %s WHERE phone_number = %s", (new_password, cust_number))
mydb.commit()
print("Password changed successfully!")
except Exception as e:
print("An error occurred:", str(e))
print("Rolling back changes...")
mydb.rollback()
# def display_message_from_trigger(cursor):
# cursor.execute("SELECT message FROM LoginAttempts WHERE message IS NOT NULL")
# result = cursor.fetchone()
# if result:
# print("Message from trigger:", result[0])
# else:
# print("No message from trigger.")
def customer_signup():
print("Customer Signup")
name = input("Enter your name: ")
while True:
try:
house_no = int(input("Enter your house number: "))
break
except ValueError:
print("Please enter a valid house number (numeric value).")
street_name = input("Enter your street name: ")
city = input("Enter your city: ")
state = input("Enter your state: ")
zip_code = input("Enter your ZIP code: ")
while True:
try:
zip_code = int(zip_code)
break
except ValueError:
print("Please enter a valid ZIP code (numeric value).")
zip_code = input("Enter your ZIP code: ")
phone_number = input("Enter your phone number: ")
email = input("Enter your email: ")
password = input("Enter your password: ")
while True:
try:
age = int(input("Enter your age: "))
break
except ValueError:
print("Please enter a valid age (numeric value).")
try:
cursor.execute("START TRANSACTION")
cursor.execute("INSERT INTO Address (House_NO, Street_Name, City, State, Zip) VALUES (%s, %s, %s, %s, %s)",
(house_no, street_name, city, state, zip_code))
address_id = cursor.lastrowid
cursor.execute("INSERT INTO Customer (customer_name, Address_ID, phone_number, email, customer_password, age) VALUES (%s, %s, %s, %s, %s, %s)",
(name, address_id, phone_number, email, password, age))
mydb.commit()
print("Customer signup successful!")
except mysql.connector.Error as err:
print("Error:", err)
print("Rolling back changes...")
mydb.rollback()
def vendor_signup():
print("Vendor Signup")
name = input("Enter vendor name: ")
email = input("Enter email: ")
vend_password = input("Enter your password: ")
while True:
try:
age = int(input("Enter age: "))
break
except ValueError:
print("Please enter a valid age (numeric value).")
phone_number = input("Enter phone number: ")
while True:
try:
phone_number = int(phone_number)
break
except ValueError:
print("Please enter a valid phone number (numeric value).")
phone_number = input("Enter phone number: ")
try:
cursor.execute("START TRANSACTION")
cursor.execute("INSERT INTO Vendor (vendor_name, Email, Age, Phone_number, vendor_password) VALUES (%s, %s, %s, %s, %s)",
(name, email, age, phone_number, vend_password))
mydb.commit()
print("Vendor signup successful!")
except mysql.connector.Error as err:
print("Error:", err)
print("Rolling back changes...")
mydb.rollback()
def delivery_agent_signup():
print("Delivery Agent Signup")
name = input("Enter delivery agent name: ")
password = input("Enter password: ")
availability = input("Enter availability (Available/Unavailable): ").capitalize()
phone_number = input("Enter phone number: ")
while True:
try:
phone_number = int(phone_number)
break
except ValueError:
print("Please enter a valid phone number (numeric value).")
phone_number = input("Enter phone number: ")
try:
# Begin transaction
cursor.execute("START TRANSACTION")
cursor.execute("INSERT INTO DeliveryAgent (da_name, da_password, availability, da_phone_no) VALUES (%s, %s, %s, %s)",
(name, password, availability, phone_number))
# Commit transaction
mydb.commit()
print("Delivery agent signup successful!")
except mysql.connector.Error as err:
# Rollback transaction if any error occurs
print("Error:", err)
print("Rolling back changes...")
mydb.rollback()
def admin_signup():
print("Admin Signup")
admin_id = input("Enter admin ID: ")
password = input("Enter password: ")
while True:
try:
admin_id = int(admin_id)
break
except ValueError:
print("Please enter a valid admin ID (numeric value).")
admin_id = input("Enter admin ID: ")
try:
# Begin transaction
cursor.execute("START TRANSACTION")
# Insert admin
cursor.execute("INSERT INTO MAIN_ADMIN (adminID, hashed_password) VALUES (%s, %s)",
(admin_id, password))
# Commit transaction
mydb.commit()
print("Admin signup successful!")
except mysql.connector.Error as err:
# Rollback transaction if any error occurs
print("Error:", err)
print("Rolling back changes...")
mydb.rollback()
def signup():
while True:
print("\nSignup as:")
print("1. Customer")
print("2. Vendor")
print("3. Delivery Agent")
print("4. Admin")
print("5. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
customer_signup()
elif choice == 2:
vendor_signup()
elif choice == 3:
delivery_agent_signup()
elif choice == 4:
admin_signup()
elif choice == 5:
print("Exiting signup...")
break
else:
print("Invalid choice. Please enter a valid option.")
homepage()
def login():
while True:
# trigger 2 message
# cursor.execute("SELECT * FROM order_summary")
# # Fetch all rows from the result set
# order_summary = cursor.fetchall()
# # Print the order_summary table
# for order in order_summary:
# print(order)
# trigger 1 message
# print("Blocked user here ---->")
# cursor.execute("SELECT * FROM LoginAttempts")
# # Fetch all rows from the result set
# login_attempts = cursor.fetchall()
# # Print the login attempts entries
# for attempt in login_attempts:
# print(attempt)
print("Enter as:")
print("1.Customer")
print("2.Vendor")
print("3.Delivery Agent")
print("4.Admin")
print("5.Exit")
print("Enter your Choice:", end=" ")
choice = int(input())
if choice == 1:
cust_number = input("Enter Customer Phone number: ")
if len(cust_number) != 10:
print("Incorrect length of phone number. Please enter a 10-digit phone number.")
continue
cursor.execute("SELECT is_banned FROM Customer WHERE phone_number = %s", (cust_number,))
is_banned_result = cursor.fetchone()
if is_banned_result and is_banned_result[0]:
print("Your account has been suspended! Please contact admin to continue.")
continue
cust_pass = input("Enter Customer Password: ")
flag_change_pass=0
while True:
cursor.execute("SELECT customer_password, incorrect_attempts FROM Customer WHERE phone_number = %s", (cust_number,))
result = cursor.fetchone()
if result:
db_cust_pass, db_incorrect_attempts = result
incorrect_attempts = 0
while cust_pass != db_cust_pass:
incorrect_attempts += 1
if incorrect_attempts >= 3:
print("Too many incorrect attempts. Your account has been suspended! Please contact admin to continue.")
cursor.execute("UPDATE Customer SET is_banned = 1, incorrect_attempts = 0 WHERE phone_number = %s", (cust_number,))
mydb.commit()
break
print("Wrong Password! Please try again.")
print("Change Password?")
change_option = input("Enter Y to change password or any other key to proceed: ").upper()
if change_option == "Y":
change_customer_password(cust_number)
flag_change_pass=1
break
cust_pass = input("Enter Customer Password: ")
if flag_change_pass == 1:
break
if incorrect_attempts >= 3:
cursor.execute("INSERT INTO LoginAttempts (message) VALUES (%s)", (cust_number,))
mydb.commit()
break
if cust_pass == db_cust_pass:
print("Customer Login Successful!")
cursor.execute("UPDATE Customer SET incorrect_attempts = 0 WHERE phone_number = %s", (cust_number,))
CustomerCommands(cust_number)
break
else:
print("No such customer found!")
elif choice == 2:
vendor_phone_number = input("Enter Vendor Phone number: ")
if len(vendor_phone_number) != 10:
print("Incorrect length of phone number. Please enter a 10-digit phone number.")
continue
cursor.execute("SELECT vendor_banned FROM Vendor WHERE Phone_number = %s", (vendor_phone_number,))
vendor_banned_result = cursor.fetchone()
if vendor_banned_result and vendor_banned_result[0]:
print("Your account has been suspended! Please contact admin to continue.")
continue
vendor_pass = input("Enter Vendor Password: ")
vendor_incorrect_attempts = 0
cursor.execute("SELECT vendor_password, vendor_incorrect_attempts FROM Vendor WHERE Phone_number = %s", (vendor_phone_number,))
v_result = cursor.fetchone()
if v_result:
db_vendor_pass, db_vendor_attempts = v_result
while vendor_pass != db_vendor_pass:
vendor_incorrect_attempts += 1
if vendor_incorrect_attempts >= 3:
print("Too many incorrect attempts. Your account has been suspended! Please contact admin to continue.")
cursor.execute("UPDATE Vendor SET vendor_banned = 1 WHERE Phone_number = %s", (vendor_phone_number,))
mydb.commit()
break
print("Wrong Password! Please try again.")
vendor_pass = input("Enter Vendor Password: ")
if vendor_incorrect_attempts < 3:
print("Vendor Login Successful!")
cursor.execute("UPDATE Vendor SET vendor_incorrect_attempts = 0 WHERE Phone_number = %s", (vendor_phone_number,))
mydb.commit()
VendorCommands(vendor_phone_number)
else:
print("No such vendor found!")
elif choice == 3:
da_name = input("Enter Delivery Agent Name: ")
da_pass = input("Enter Delivery Agent Password: ")
cursor.execute("SELECT da_name FROM DeliveryAgent WHERE da_name = %s AND da_password = %s", (da_name, da_pass))
result = cursor.fetchone()
if result:
print("Delivery Agent Login Successful!")
DeliveryAgentCommands()
else:
print("No such delivery agent found!")
elif choice == 4:
admin_id = int(input("Enter Admin ID: "))
admin_pass = input("Enter Admin Password: ")
cursor.execute("SELECT hashed_password FROM MAIN_ADMIN WHERE adminID = %s", (admin_id,))
result = cursor.fetchone()
if result:
db_admin_pass = result[0]
if db_admin_pass == admin_pass:
print("Admin Login Successful!")
AdminCommands()
else:
print("Wrong Password!")
else:
print("No such admin found!")
elif choice == 5:
break
homepage()
def CustomerCommands(customer_number):
while (True):
cursor.execute("SELECT customer_id FROM Customer WHERE phone_number = %s", (customer_number,))
result1 = cursor.fetchone()
cust_id = result1[0]
print("1. View Books")
print("2. Search")
print("3. Add books to cart")
print("4. Show order history")
print("5. Show Cart")
print("6. Show your personal details")
print("7. Show book reviews given by you")
print("8. Show DA_Agent reviews given by you")
print("9. Place order")
print("10. Clear current cart ")
print("11. Know more about any book")
print("12. Give book reviews")
print("13. Logout")
choice = int(input("Enter your choice: "))
if choice == 1:
cursor.execute("SELECT b.*, r.rating, r.content FROM Book b LEFT JOIN ProductReview r ON b.book_id = r.book_id")
books_with_reviews = cursor.fetchall()
if books_with_reviews:
print("All Books:")
for book in books_with_reviews:
print("Book ID:", book[0])
print("Title:", book[1])
print("Author:", book[2])
print("Genre:", book[3])
print("Series:", book[4])
print("Publication:", book[5])
print("Availability:", book[6])
print("Vendor ID:", book[7])
print("Price:", book[8])
print("Rating:", book[9])
print()
else:
print("No books found in the database.")
elif choice == 2:
# Search logic
search_filters = {}
book_id = input("Enter Book ID (press Enter to skip): ")
if book_id:
search_filters['book_id'] = book_id
title = input("Enter Title (press Enter to skip): ")
if title:
search_filters['book_title'] = title
author = input("Enter Author (press Enter to skip): ")
if author:
search_filters['book_author'] = author
genre = input("Enter Genre (press Enter to skip): ")
if genre:
search_filters['book_genre'] = genre
series = input("Enter Series (press Enter to skip): ")
if series:
search_filters['book_series'] = series
publication = input("Enter Publication (press Enter to skip): ")
if publication:
search_filters['book_publication'] = publication
availability = input("Enter Availability (press Enter to skip): ")
if availability:
search_filters['book_availability'] = availability
vendor_id = input("Enter Vendor ID (press Enter to skip): ")
if vendor_id:
search_filters['VendorID'] = vendor_id
price = input("Enter Price (press Enter to skip): ")
if price:
search_filters['book_price'] = price
sql_query = "SELECT * FROM Book WHERE "
conditions = []
for key, value in search_filters.items():
if key == 'book_price':
conditions.append(f"{key} < {value}")
else:
conditions.append(f"{key} = '{value}'")
sql_query += " OR ".join(conditions)
print(sql_query)
cursor.execute(sql_query)
search_results = cursor.fetchall()
if search_results:
print("Search Results:")
for book in search_results:
print("Book ID:", book[0])
print("Title:", book[1])
print("Author:", book[2])
print("Genre:", book[3])
print("Series:", book[4])
print("Publication:", book[5])
print("Availability:", book[6])
print("Vendor ID:", book[7])
print("Price:", book[8])
print()
else:
print("No books found matching the provided criteria.")
elif choice == 3:
cursor.execute("SELECT cart_id FROM Cart WHERE customer_id = %s", (cust_id,))
cart_info = cursor.fetchone()
if cart_info:
cart_id = cart_info[0]
flag = 0
else:
flag = 1
book_id = input("Enter the Book ID you want to add to your cart: ")
quantity = int(input("Enter the quantity: "))
cursor.execute("SELECT * FROM Book WHERE book_id = %s", (book_id,))
book = cursor.fetchone()
if book:
if book[6] >= quantity:
cursor.execute("SELECT quantity FROM Cart WHERE customer_id = %s AND book_id = %s", (cust_id, book_id))
existing_quantity = cursor.fetchone()
if existing_quantity:
new_quantity = existing_quantity[0] + quantity
cursor.execute("UPDATE Cart SET quantity = %s WHERE customer_id = %s AND book_id = %s",
(new_quantity, cust_id, book_id))
else:
if flag == 0:
cursor.execute("INSERT INTO Cart (cart_id, customer_id, book_id, quantity) VALUES (%s, %s, %s, %s)",
(cart_id, cust_id, book_id, quantity))
else:
cursor.execute("INSERT INTO Cart (customer_id, book_id, quantity) VALUES (%s, %s, %s)",
(cust_id, book_id, quantity))
mydb.commit()
print("Book added to cart successfully!")
else:
print("Sorry, the requested quantity is not available.")
else:
print("Book not found.")
elif choice == 4:
cursor.execute("SELECT * FROM customer_order WHERE customer_id = %s", (cust_id,))
orders = cursor.fetchall()
if orders:
print("Your Order History:")
for order in orders:
order_id = order[0]
order_date = order[1]
total_price = order[3]
order_status = order[4]
address_id = order[5]
payment_mode = order[6]
cursor.execute("SELECT * FROM Address WHERE Address_ID = %s", (address_id,))
address_info = cursor.fetchone()
if address_info:
house_no = address_info[1]
street_name = address_info[2]
city = address_info[3]
state = address_info[4]
zip_code = address_info[5]
print("Order ID:", order_id)
print("Order date:", order_date)
print("Total Price:", total_price)
print("Order status:", order_status)
print("Address:")
print("House Number:", house_no)
print("Street Name:", street_name)
print("City:", city)
print("State:", state)
print("Zip Code:", zip_code)
print("Payment Mode:", payment_mode)
print()
else:
print("Address details not found for Order ID:", order_id)
else:
print("You haven't placed any orders yet.")
elif choice == 5:
cursor.execute("SELECT c.book_id, b.book_price, c.quantity FROM Cart c JOIN Book b ON c.book_id = b.book_id WHERE c.customer_id = %s", (cust_id,))
cart_items = cursor.fetchall()
if cart_items:
total_value = 0
print("Your Cart:")
for item in cart_items:
book_id = item[0]
price = item[1]
quantity = item[2]
total_item_value = price * quantity
total_value += total_item_value
print("Book ID:", book_id)
print("Quantity:", quantity)
print("Item Value:", price)
print()
print("Total Cart Value:", total_value)
else:
print("Your cart is empty.")
elif choice == 6:
cursor.execute("SELECT * FROM Customer WHERE phone_number = %s", (customer_number,))
user_info = cursor.fetchone()
if user_info:
print("Personal Information:")
print("Customer ID:", user_info[0])
print("Customer Name:", user_info[1])
print("Address ID:", user_info[2])
print("Phone Number:", user_info[3])
print("Email:", user_info[4])
print("Age:", user_info[6])
else:
print("User not found.")
pass
elif choice == 7:
try:
# Begin transaction
cursor.execute("START TRANSACTION")
# Execute SQL query to fetch book reviews given by the customer
cursor.execute("SELECT * FROM ProductReview WHERE customer_id = %s", (cust_id,))
reviews = cursor.fetchall()
if reviews:
print("Your Book Reviews:")
for review in reviews:
print("Review ID:", review[0])
print("Book ID:", review[1])
print("Rating:", review[3])
print("Content:", review[4])
print()
else:
print("You haven't given any book reviews yet.")
# Commit transaction
mydb.commit()
print("Transaction successful!")
except mysql.connector.Error as err:
# Rollback transaction if any error occurs
print("Error:", err)
print("Rolling back changes...")
mydb.rollback()
elif choice == 8:
cursor.execute("SELECT * FROM DAgentReview WHERE customer_id = %s", (cust_id,))
da_agent_reviews = cursor.fetchall()
if da_agent_reviews:
print("Your Delivery Agent Reviews:")
for review in da_agent_reviews:
review_id = review[0]
da_id = review[1]
agent_review = review[3]
agent_review_description = review[4]
agent_review_date = review[5]
print("Review ID:", review_id)
print("Agent Review out of 5:", agent_review)
print("Review Description:", agent_review_description)
print("Review Date:", agent_review_date)
print()
else:
print("You haven't given any delivery agent reviews yet.")
elif choice == 9:
cursor.execute("SELECT * FROM Cart WHERE customer_id = %s", (cust_id,))
cart_items = cursor.fetchall()
if not cart_items:
print("Your cart is empty. Please add items to your cart before placing an order.")
continue
else:
cursor.execute("SELECT c.book_id, b.book_price, c.quantity, b.book_availability FROM Cart c JOIN Book b ON c.book_id = b.book_id WHERE c.customer_id = %s", (cust_id,))
cart_items = cursor.fetchall()
if cart_items:
total_value = 0
can_place_order = True
for item in cart_items:
book_id = item[0]
price = item[1]
quantity = item[2]
stock_quantity = item[3]
if quantity > stock_quantity:
print(f"Sorry, the quantity of book with ID {book_id} is insufficient.")
can_place_order = False
break
total_item_value = price * quantity
total_value += total_item_value
if not can_place_order:
print("Your order cannot be placed due to insufficient quantity.")
continue
print("Total Cart Value:", total_value)
address_choice = input("Do you want to use your saved address? (yes/no): ").lower()
if address_choice == "yes":
cursor.execute("SELECT Address_ID FROM Customer WHERE customer_id = %s", (cust_id,))
address_id = cursor.fetchone()[0]
cursor.execute("SELECT * FROM Address WHERE Address_ID = %s", (address_id,))
address_info = cursor.fetchone()
if address_info:
print("Your Saved Address:")
print("House Number:", address_info[1])
print("Street Name:", address_info[2])
print("City:", address_info[3])
print("State:", address_info[4])
print("Zip Code:", address_info[5])
else:
print("You haven't saved any address yet.")
elif address_choice == "no":
house_no = int(input("Enter your house number: "))
street_name = input("Enter your street name (press Enter to skip): ")
city = input("Enter your city: ")
state = input("Enter your state: ")
zip_code = int(input("Enter your zip code: "))
cursor.execute("INSERT INTO Address (House_NO, Street_Name, City, State, Zip) VALUES (%s, %s, %s, %s, %s)",
(house_no, street_name, city, state, zip_code))
mydb.commit()
address_id = cursor.lastrowid
else:
print("Invalid choice. Please enter 'yes' or 'no'.")
payment_mode = input("Enter mode of payment (e.g., Credit Card, PayPal, etc.): ")
cursor.execute("INSERT INTO customer_order (customer_id, total_price, address, payment_mode) VALUES (%s, %s, %s, %s)",
(cust_id, total_value, address_id, payment_mode))
order_id = cursor.lastrowid
for item in cart_items:
book_id = item[0]
quantity = item[2]
cursor.execute("UPDATE Book SET book_availability = book_availability - %s WHERE book_id = %s", (quantity, book_id))
for item in cart_items:
book_id = item[0]
quantity = item[2]
cursor.execute("INSERT INTO OrderItem (book_id, order_id, quantity) VALUES (%s, %s, %s)",
(book_id, order_id, quantity))
# Clear the cart after placing the order
cursor.execute("DELETE FROM Cart WHERE customer_id = %s", (cust_id,))
mydb.commit()
print("Your order has been successfully placed. Thank you for shopping with us!")
# Display order summary
# cursor.execute("CALL GenerateOrderSummary(%s)", (order_id,))
# order_summary = cursor.fetchall()
# for summary in order_summary:
# print(summary)
elif choice == 10:
try:
cursor.execute("START TRANSACTION;")
# Delete the cart for the given customer_id
cursor.execute("DELETE FROM Cart WHERE customer_id = %s", (cust_id,))
mydb.commit()
cursor.execute("INSERT INTO Cart (customer_id) VALUES (%s)", (cust_id,))
mydb.commit()
print("Your cart has been cleared and a new cart has been created.")
except mysql.connector.Error as e:
print("Error:", str(e))
mydb.rollback() # Rollback the transaction in case of an error
if choice == 11:
book_id = int(input("Enter the Book ID: "))
cursor.execute("SELECT b.*, bd.book_description FROM Book b LEFT JOIN BookDescription bd ON b.book_id = bd.book_id WHERE b.book_id = %s", (book_id,))
book_details = cursor.fetchone()
if book_details:
print("Book Details:")
print("Book ID:", book_details[0])
print("Title:", book_details[1])
print("Author:", book_details[2])
print("Genre:", book_details[3])
print("Series:", book_details[4])
print("Publication:", book_details[5])
print("Availability:", book_details[6])
print("Vendor ID:", book_details[7])
print("Price:", book_details[8])
print("Description:", book_details[9])
print()
cursor.execute("SELECT rating, content FROM ProductReview WHERE book_id = %s", (book_id,))
reviews = cursor.fetchall()
if reviews:
print("Reviews:")
for review in reviews:
print("Rating:", review[0])
print("Content:", review[1])
print()
else:
print("No reviews found for this book.")
else:
print("Book not found.")
if choice == 12:
try:
# Begin transaction
cursor.execute("START TRANSACTION")
# Fetch orders placed by the customer
cursor.execute("SELECT order_id FROM customer_order WHERE customer_id = %s", (cust_id,))
orders = cursor.fetchall()
if orders:
print("Your Orders:")
for order in orders:
order_id = order[0]
cursor.execute("SELECT b.book_id, b.book_title FROM OrderItem oi JOIN Book b ON oi.book_id = b.book_id WHERE oi.order_id = %s", (order_id,))
books_in_order = cursor.fetchall()
if books_in_order:
print(f"Order ID: {order_id}")
print("Books Purchased:")
for book in books_in_order:
print(f"Book ID: {book[0]}, Title: {book[1]}")
rating = int(input("Enter rating (0-5): "))
content = input("Enter review content: ")
cursor.execute("INSERT INTO ProductReview (book_id, customer_id, rating, content) VALUES (%s, %s, %s, %s)", (book[0], cust_id, rating, content))
else:
print("You haven't placed any orders yet.")
# Commit transaction
mydb.commit()
print("Transaction successful!")
except mysql.connector.Error as err:
# Rollback transaction if any error occurs
print("Error:", err)
print("Rolling back changes...")
mydb.rollback()
elif choice == 13:
print("Signing out...")
break
else:
continue
login()
def delete_book(vendor_id, book_id, quantity):
try:
lock.acquire()
# Start a transaction
cursor.execute("START TRANSACTION")
# Check if the book belongs to the vendor and has enough quantity to delete
cursor.execute("SELECT book_availability FROM Book WHERE book_id = %s AND VendorID = %s FOR UPDATE", (book_id, vendor_id))
book = cursor.fetchone()
if book:
current_quantity = book[0]
if current_quantity >= quantity:
# Perform the deletion
new_quantity = current_quantity - quantity
cursor.execute("UPDATE Book SET book_availability = %s WHERE book_id = %s AND VendorID = %s", (new_quantity, book_id, vendor_id))
print(f"{quantity} books deleted successfully.")
else:
print("Insufficient quantity to delete.")
else:
print("Invalid Book ID or book belongs to another vendor.")
# Commit the transaction
mydb.commit()
except mysql.connector.Error as err:
# Rollback the transaction if an error occurs
mydb.rollback()
print("Error:", err)
finally:
# Release the lock
lock.release()
def VendorCommands(vendor_number):
while True:
cursor.execute("SELECT VendorID FROM Vendor WHERE phone_number = %s", (vendor_number,))
vendor_id = cursor.fetchone()[0]
print("1. View Vendor Books")
print("2. Search")
print("3. Add book")
print("4. Delete book")
print("5. Edit book stock")
print("6. Show your personal details")
print("7. Logout")
choice = input("Enter your choice: ")
if choice == '1':
cursor.execute("SELECT * FROM Book WHERE VendorID = %s", (vendor_id,))
books = cursor.fetchall()