-
Notifications
You must be signed in to change notification settings - Fork 0
/
record_SQL_data.py
90 lines (80 loc) · 2.53 KB
/
record_SQL_data.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
import mysql.connector
import time
import datetime
def query_all_in_table(mydb,mycursor,database,tableName):
"""
Queries tableName and returns the value of the entire query.
database = name of database
tableName = name of table in database.table
"""
sql = "SELECT * FROM "+database+"."+tableName
try:
mycursor.execute(sql)
query=mycursor.fetchall()
return query
except:
mydb.rollback()
print("Issue occurred when trying to query table.")
return -1
def add_to_brew_list(mydb,mycursor,brewName):
"""
Adds a record to beerCode.brewList.
brewName = String describing the name of the beer.
"""
ts = time.time()
timestamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
sql = "INSERT INTO beerCode.brewList (brewName, brewDate) VALUES (%s, %s)"
val = (brewName, timestamp)
try:
mycursor.execute(sql, val)
mydb.commit()
print(mycursor.rowcount, "Record inserted.")
except:
mydb.rollback()
print("Issue occurred when trying to write record.")
def remove_from_brew_list(mydb,mycursor,brewIDs):
"""
Removes list of brewIDs from beerCode.brewList
brewIDs = array of IDs to remove from table.
"""
try:
for brewID in brewIDs:
sql = "DELETE FROM beerCode.brewList WHERE brewID = "+str(brewID)
mycursor.execute(sql)
mydb.commit()
print(mycursor.rowcount, "record(s) deleted")
except:
mydb.rollback()
print("Issue ocurred when trying to delete record(s).")
def add_to_temp_log(mydb,mycursor,brewID,time,tempAir,tempLiquid,op_hot,op_cold):
"""
Adds a record to beerCode.brewList.
brewID = Integer mapping to brewList
time = Timestamp value of time of measurement
tempAir = Double value of tempAir sensor
tempLiquid = Double value of tempLiquid sensor
"""
sql = "INSERT INTO beerCode.brewTemperatureHistory (brewID,timeMeasurement,tempAir,tempLiquid,op_hot,op_cold) VALUES (%s,%s,%s,%s,%s,%s)"
print(sql)
val = (brewID,time,tempAir,tempLiquid,op_hot,op_cold)
try:
mycursor.execute(sql, val)
mydb.commit()
print(mycursor.rowcount, "Record inserted.")
except:
mydb.rollback()
print("Issue occurred when trying to write record.")
def remove_from_temp_log(mydb,mycursor,tempIDs):
"""
Removes list of tempIDs from beerCode.brewTemperatureHistory
tempIDs = array of IDs to remove from table.
"""
try:
for tempID in tempIDs:
sql = "DELETE FROM beerCode.brewTemperatureHistory WHERE tempID = "+str(tempID)
mycursor.execute(sql)
mydb.commit()
print(mycursor.rowcount, "record(s) deleted")
except:
mydb.rollback()
print("Issue ocurred when trying to delete record(s).")