-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.py
149 lines (128 loc) · 5.03 KB
/
backend.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import pymysql
from pprint import PrettyPrinter
pp=PrettyPrinter(indent=4)
def __connect__():
conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='password', db='zips')
cur = conn.cursor()
return cur
def __connectEmps__():
conn=pymysql.connect(host='localhost',port=3306,user='root',passwd='password',db='employees')
cur=conn.cursor()
return cur
def query1():
cur=__connect__()
sql='''SELECT state, SUM(pop) AS totalPop
FROM zipCodes
GROUP BY state
HAVING totalPop >= (10*1000*1000)'''
cur.execute(sql);
res='''<table class='table table-bordered table-hover'>
<tr><th>State</th>
<th>Population</th></tr>'''
for row in cur:
res+="<tr><td>"+row[0]+"</td><td>"+str(row[1])+"</td>"
return res
def query2():
cur=__connect__()
sql=""" (SELECT state, AVG(pop) as avgPop FROM zipCodes GROUP BY state)"""
cur.execute(sql);
res='''<table class='table table-bordered table-hover'>
<tr><th>State</th>
<th> Average City Population</th></tr>'''
for row in cur:
res+="<tr><td>"+row[0]+"</td><td>"+str(row[1])+"</td>\n"
return res
def query3():
cur=__connect__()
sql1='''SELECT
t.state,t.city,t.pop
FROM
zipCodes t
JOIN
( SELECT state
, MAX(pop) AS Population
FROM zipCodes
GROUP BY state
) maxp
ON maxp.state = t.state
AND maxp.Population = t.pop'''
sql2='''SELECT
t.state,t.city,t.pop
FROM
zipCodes t
JOIN
( SELECT state
, MIN(pop) AS Population
FROM zipCodes
GROUP BY state
) minp
ON minp.state = t.state
AND minp.Population = t.pop
'''
sql=sql1+" UNION ALL "+sql2
cur.execute(sql);
res='''<table class='table table-bordered table-hover'>
<tr><th>State</th>
<th>City</th>
<th>Population</th></tr>'''
for row in cur:
res+="<tr><td>"+str(row[0])+"</td><td>"+str(row[1])+"</td>"+"<td>"+str(row[2])+"</td></tr>"
return res
def query4():
cur=__connect__()
sql='''SELECT state, city From zipCodes where city='SPRINGFIELD' GROUP BY state'''
cur.execute(sql)
res='''<table class='table table-bordered table-hover'>
<tr><th>State</th>
<th>City</th></tr>'''
for row in cur:
res+="<tr><td>"+str(row[0])+"</td><td>"+str(row[1])+"</td></tr>"
return res
def empQuery1():
cur=__connectEmps__()
sql='''SELECT first_name,last_name,salary from salaries JOIN employees ON salaries.emp_no=employees.emp_no WHERE salaries.to_date='9999-01-01' ORDER BY salary DESC LIMIT 20'''
cur.execute(sql)
res='''<table class='table table-bordered table-hover'>
<tr><th>First Name</th>
<th>Last Name </th>
<th>Salary</th></tr>'''
for row in cur:
res+="<tr><td>"+str(row[0])+"</td><td>"+str(row[1])+"</td><td>"+str(row[2])+"</td></tr>"
return res
def empQuery2():
cur=__connectEmps__()
sql='''select first_name,last_name,salary,dept_name from salaries join employees on salaries.emp_no=employees.emp_no join dept_emp on employees.emp_no=dept_emp.emp_no join departments on dept_emp.dept_no=departments.dept_no WHERE salaries.to_date='9999-01-01' AND departments.dept_name='Development' ORDER BY salaries.salary DESC LIMIT 10;'''
cur.execute(sql)
res='''<table class='table table-bordered table-hover'>
<tr><th>First Name</th>
<th>Last Name </th>
<th>Salary</th>
<th>Department</th></tr>'''
for row in cur:
res+="<tr><td>"+str(row[0])+"</td><td>"+str(row[1])+"</td><td>"+str(row[2])+"</td><td>"+str(row[3])+"</td></tr>"
return res
def empQuery3():
cur=__connectEmps__()
sql='''select first_name,last_name,birth_date from employees order by birth_date asc limit 10'''
cur.execute(sql)
res='''<table class='table table-bordered table-hover'>
<tr><th>First Name</th>
<th>Last Name </th>
<th>Birth Date</th></tr>'''
for row in cur:
res+="<tr><td>"+str(row[0])+"</td><td>"+str(row[1])+"</td><td>"+str(row[2])+"</td></tr>"
return res
def empQuery4():
cur=__connectEmps__()
sql='''select first_name, last_name, salary, dept_name from departments join dept_manager on departments.dept_no=dept_manager.dept_no join employees on dept_manager.emp_no=employees.emp_no join salaries on employees.emp_no= salaries.emp_no where salaries.to_date='9999-01-01' order by salaries.salary desc limit 10'''
cur.execute(sql)
res='''<table class='table table-bordered table-hover'>
<tr><th>First Name</th>
<th>Last Name </th>
<th>Salary</th>
<th>Department</th></tr>'''
for row in cur:
res+="<tr><td>"+str(row[0])+"</td><td>"+str(row[1])+"</td><td>"+str(row[2])+"</td><td>"+str(row[3])+"</td></tr>"
return res
if __name__=="__main__":
pp.pprint(empQuery1())