-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmbeddedQueries.py
94 lines (70 loc) · 2.14 KB
/
EmbeddedQueries.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
from rich.console import Console
from rich.table import Table
import mysql.connector
db= mysql.connector.connect(
host= "localhost",
user= "root",
password= "Mohitbhai",
database= "ApniDukan"
)
myCursor= db.cursor()
def execute(query):
# try catch block
try:
myCursor.execute(query)
except mysql.connector.errors.ProgrammingError:
print("\nInvalid query!")
return
myResult= myCursor.fetchall()
table= Table(show_header= True, header_style= "bold blue")
for x in myCursor.description:
table.add_column(x[0], style= "blue", width= 20)
for x in myResult:
table.add_row(*[str(i) for i in x])
console= Console()
console.print(table)
# Display the number of products with rating more than 4 in each category
query1= """
SELECT
category_name, COUNT(product_ID)
FROM
(Category
INNER JOIN Product USING (category_ID))
WHERE
product_rating > 4
GROUP BY category_ID;"""
# Display all the products with rating 5
query2= """
SELECT
product_ID, product_name, product_price, product_rating, category_ID
FROM
product
WHERE
product_rating = 5;"""
print()
menuTable= Table(show_header= True, header_style= "bold white")
menuTable.add_column("Query Number", style= "white", width= 20)
menuTable.add_column("Query", style= "white", width= 100)
menuTable.add_row("1", "Display the number of products with rating more than 4 in each category")
menuTable.add_row("2", "Display all the products with rating 5")
menuTable.add_row("3", "Custom Query")
menuTable.add_row("4", "Exit")
console= Console()
console.print(menuTable)
choice= int(input("Enter your choice: "))
while(choice!=4):
print()
if (choice == 1):
execute(query1)
elif (choice == 2):
execute(query2)
elif (choice == 3):
query= input("Enter your query: ")
execute(query)
elif (choice == 4):
break
else:
print("Invalid choice!")
print()
print()
choice= int(input("Enter your choice: "))