-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
executable file
·67 lines (56 loc) · 1.71 KB
/
utils.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
import time
import pymysql
def get_time():
time_str = time.strftime("%Y{}%m{}%d{} %X")
return time_str.format("年","月","日")
def get_conn():
# 建立連接
conn = pymysql.connect(host="127.0.0.1", user="root", password="123", db="cov", charset="utf8")
cursor = conn.cursor()
return conn, cursor
def close_conn(conn, cursor):
if cursor:
cursor.close()
if conn:
conn.close()
def query(sql,*args):
"""
:param sql:
:param args:
:return:
"""
conn,cursor = get_conn()
cursor.execute(sql,args)
res = cursor.fetchall()
close_conn(conn,cursor)
return res
def get_c1_data():
sql = "select `confirm`,`isolated`,`dead`,`unblock` from history order by ds desc limit 1"
res = query(sql)
return res[0]
def get_c2_data():
sql = "select `ds`,`confirm`,`isolated`,`dead`,`unblock` from history order by ds desc limit 10"
res = query(sql)
return res
def get_map_data():
sql_city = "select city from details group by city"
sql_results = "select * from details where city=%s order by update_time desc limit 1"
cities = query(sql_city) # 先找出所有縣市
results = []
for city in cities:
res = query(sql_results, city)
results.append(res[0])
return results
def get_c3_data():
sql_city = "select city from details group by city"
cities = query(sql_city)
sql_results = "select confirm_add from details where city=%s order by update_time desc limit 7"
results = {}
for city in cities:
city = city[0]
res = query(sql_results, city)
res = [x[0] for x in res]
results[city] = res
return results
if __name__ == "__main__":
print(get_c3_data())