-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
118 lines (95 loc) · 3.05 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
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
import time
import pymysql
from decimal import Decimal
import json
def get_time():
time_str = time.strftime("%Y{}%m{}%d{} %X")
return time_str.format("年", "月", "日")
def get_conn():
"""
:return: 连接,游标
"""
# 创建连接
conn = pymysql.connect(host="localhost",
user="root",
password="123456",
db="cov",
charset="utf8")
# 创建游标
cursor = conn.cursor() # 执行完毕返回的结果集默认以元组显示
return conn, cursor
def close_conn(conn, cursor):
cursor.close()
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():
"""
:return: 返回大屏div id=c1 的数据
"""
# 因为会更新多次数据,取时间戳最新的那组数据
sql = "select sum(confirm)," \
"(select suspect from history order by ds desc limit 1)," \
"sum(heal)," \
"sum(dead) " \
"from details " \
"where update_time=(select update_time from details order by update_time desc limit 1) "
#"where update_time=(select update_time from details order by update_time desc limit 1) "
res = query(sql)
res_list = [str(i) for i in res[0]]
res_tuple = tuple(res_list)
return res_tuple
def get_c2_data():
"""
:return: 返回各省数据
"""
# 因为会更新多次数据,取时间戳最新的那组数据
sql = "select province,sum(confirm) from details " \
"where update_time=(select update_time from details " \
"order by update_time desc limit 1) " \
"group by province"
res = query(sql)
return res
def get_l1_data():
sql = "select ds,confirm,suspect,heal,dead from history"
res = query(sql)
return res
def get_l2_data():
sql = "select ds,confirm_add,suspect_add from history"
res = query(sql)
return res
def get_r1_data():
"""
:return: 返回非湖北地区城市确诊人数前5名
"""
sql = 'SELECT province,confirm_add FROM ' \
'(select province,sum(confirm_add) as confirm_add from details ' \
'where update_time=(select update_time from details order by update_time desc limit 1) ' \
'group by province) as a ' \
'ORDER BY confirm_add DESC LIMIT 5'
res = query(sql)
return res
def get_r2_data():
'''
获取世界各国的疫情数据
:return:
'''
# 因为会更新多次数据,取时间戳最新的那组数据
sql = "select province,sum(confirm_add) from details " \
"where update_time=(select update_time from details " \
"order by update_time desc limit 1) " \
"group by province"
res = query(sql)
return res
if __name__ == "__main__":
print(get_c1_data())