-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
85 lines (74 loc) · 2.5 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
82
83
84
85
#!/usr/bin/python3
import MySQLdb, datetime, json, os
class mysql_database:
def __init__(self):
credentials_file = os.path.join(os.path.dirname(__file__), "credentials.mysql")
f = open(credentials_file, "r")
credentials = json.load(f)
f.close()
for key, value in credentials.items(): # remove whitespace
credentials[key] = value.strip()
self.connection = MySQLdb.connect(
user=credentials["USERNAME"],
password=credentials["PASSWORD"],
database=credentials["DATABASE"],
)
self.cursor = self.connection.cursor()
def execute(self, query, params=[]):
try:
self.cursor.execute(query, params)
self.connection.commit()
except:
self.connection.rollback()
raise
def query(self, query):
cursor = self.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute(query)
return cursor.fetchall()
def __del__(self):
self.connection.close()
class weather_database:
def __init__(self):
self.db = mysql_database()
self.insert_template = "INSERT INTO WEATHER_MEASUREMENT (AMBIENT_TEMPERATURE, GROUND_TEMPERATURE, AIR_QUALITY, AIR_PRESSURE, HUMIDITY, WIND_DIRECTION, WIND_SPEED, WIND_GUST_SPEED, RAINFALL, CREATED) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s);"
self.update_template = (
"UPDATE WEATHER_MEASUREMENT SET REMOTE_ID=%s WHERE ID=%s;"
)
self.upload_select_template = (
"SELECT * FROM WEATHER_MEASUREMENT WHERE REMOTE_ID IS NULL;"
)
def is_number(self, s):
try:
float(s)
return True
except ValueError:
return False
def is_none(self, val):
return val if val != None else "NULL"
def insert(
self,
ambient_temperature,
ground_temperature,
air_quality,
air_pressure,
humidity,
wind_direction,
wind_speed,
wind_gust_speed,
rainfall,
created=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
):
params = (
ambient_temperature,
ground_temperature,
air_quality,
air_pressure,
humidity,
wind_direction,
wind_speed,
wind_gust_speed,
rainfall,
created,
)
print(self.insert_template % params)
self.db.execute(self.insert_template, params)