-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmysql_getdata.py
More file actions
67 lines (57 loc) · 2 KB
/
mysql_getdata.py
File metadata and controls
67 lines (57 loc) · 2 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
import datetime
import pymysql
from threading import *
class BD:
def __init__(self, bd_name):
self.con = pymysql.connect('localhost', 'root',
'toor', bd_name)
def get_day(self, id, date):
with self.con:
cur = self.con.cursor()
s = "SELECT * FROM unique_visitors WHERE id={} and date=\"{}\""
cur.execute(s.format(id, date))
count = cur.rowcount
return count
def get_month(self, id, date):
with self.con:
cur = self.con.cursor()
s = "SELECT * FROM unique_visitors " \
"WHERE id={} and date LIKE \"{}\""
cur.execute(s.format(id, "%" + date[3:]))
count = cur.rowcount
return count
def get_year(self, id, date):
with self.con:
cur = self.con.cursor()
s = "SELECT * FROM unique_visitors " \
"WHERE id={} and date LIKE \"{}\""
cur.execute(s.format(id, "%" + date[6:]))
count = cur.rowcount
return count
def get_all(self, id):
with self.con:
cur = self.con.cursor()
cur.execute("SELECT * FROM unique_visitors WHERE id=%s", id)
count = cur.rowcount
return count
def get_next_id(self):
lock = Lock()
lock.acquire()
try:
with self.con:
cur = self.con.cursor()
cur.execute("SELECT * FROM next_id")
next_id = cur.fetchone()[0]
s = "UPDATE next_id SET id={} WHERE id={}"
cur.execute(s.format(next_id + 1, next_id))
return next_id
finally:
lock.release()
def set_visit(self, id, date):
with self.con:
cur = self.con.cursor()
s = "insert into unique_visitors (id,date) value ("
cur.execute(s + str(id) + ",\"" + date + "\")")
def get_date():
now = datetime.datetime.now()
return now.strftime("%d.%m.%Y")