-
Notifications
You must be signed in to change notification settings - Fork 1
/
save_to_db.py
60 lines (50 loc) · 1.51 KB
/
save_to_db.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
# inserts mqtt data that is stored in a csv file to the database
# Only the last line
import os
import csv
file = '/var/www/html/mqttstats.csv'
#with open('/var/www/html/mqttstats.csv', 'rb') as f:
# try: # catch OSError in case of a one line file
# f.seek(-2, os.SEEK_END)
# while f.read(1) != b'\n':
# f.seek(-2, os.SEEK_CUR)
# except OSError:
# f.seek(0)
# last_line = f.readline().decode()
#print('last csv line: ',last_line)
with open(file) as f:
try:
reader = csv.reader(f)
data = list(reader)
except IOError:
print('File error')
last_line_list=(data[len(data) -1])
#print(last_line_list)
new_last_line_list=[last_line_list[0],last_line_list[1],last_line_list[2],last_line_list[3],last_line_list[4],'20']
print(new_last_line_list)
import mysql.connector
try:
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="******",
database="mqtt"
)
#print(mydb)
mycursor = mydb.cursor()
except mysql.connector.Error as err:
print(err)
print("Error Code:", err.errno)
print("SQLSTATE", err.sqlstate)
print("Message", err.msg)
sql = "INSERT INTO sensor (date, outtemp, hum, wtemp, power, intemp) VALUES (%s, %s, %s, %s, %s, %s)"
val = (new_last_line_list)
try:
mycursor.execute(sql, val)
mydb.commit()
mydb.close()
except mysql.connector.Error as err:
print(err)
print("Error Code:", err.errno)
print("SQLSTATE", err.sqlstate)
print("Message", err.msg)