/
database.py
81 lines (58 loc) · 1.76 KB
/
database.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
'''
Helper for database access.
'''
import mysql.connector
import threading
def createConnection():
connection = mysql.connector.connect(
user='door', password='door',
host='127.0.0.1',
database='door')
return connection;
def containsCard(code):
connection = createConnection()
cursor = connection.cursor()
query = ("SELECT * FROM card where code = %s")
data = (code,)
cursor.execute(query, data, False)
result = cursor.fetchone()
cursor.close()
connection.close()
if result is None:
return False
return True
def log_card_async(code, success):
thread = threading.Thread(target=log_card, args=(code, success,))
thread.start()
def log_card(code, success):
connection = createConnection()
cursor = connection.cursor()
query = ("INSERT INTO log_card (code, success, timestamp) VALUES (%s, %s, now())")
data = (code, success,)
cursor.execute(query, data, False)
connection.commit()
cursor.close()
connection.close()
def log_button_async():
thread = threading.Thread(target=log_button)
thread.start()
def log_button():
connection = createConnection()
cursor = connection.cursor()
query = ("INSERT INTO log_button (timestamp) VALUES (now())")
cursor.execute(query, multi=False)
connection.commit()
cursor.close()
connection.close()
def log_door_async(info):
thread = threading.Thread(target=log_door, args=(info,))
thread.start()
def log_door(info):
connection = createConnection()
cursor = connection.cursor()
query = ("INSERT INTO log_door (info, timestamp) VALUES (%s, now())")
data = (info,)
cursor.execute(query, data, multi=False)
connection.commit()
cursor.close()
connection.close()