Last active
April 8, 2021 06:53
-
-
Save ulrischa/1177fe0bf7adaf8aa0f3e7fe63f80516 to your computer and use it in GitHub Desktop.
Save Enviro Data to DB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mysql.connector | |
def save_enviro_data_in_db(vals): | |
print ("save db") | |
try: | |
mydb = mysql.connector.connect( | |
host="", | |
user="", | |
password="", | |
database="", | |
port = 3306, | |
connection_timeout= 60 | |
) | |
cur_time = int(time.time()) | |
mycursor = mydb.cursor() | |
vals['pressure'] = (float(vals['pressure'])/100) / (1-0.0065*506/(float(vals["temperature"])+273.15))**(0.03416/0.0065) | |
print (vals['pressure']) | |
vals['pressure'] = "{:.2f}".format(vals['pressure']) | |
sql = "INSERT INTO TABELLENNAME (timestamp, p1, p2, temperature, pressure, humidity, lux, proximity, nh3, oxidising, reducing, adc, amp_100_200, amp_500_600, amp_1000_1200, cpu_temp) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s);" | |
valin = (cur_time,vals["P1"],vals["P2"],vals["temperature"],vals["pressure"],vals["humidity"],vals["lux"],vals["proximity"],vals["nh3"],vals["oxidising"],vals["reducing"],vals["adc"],vals["amp_100_200"],vals["amp_500_600"],vals["amp_1000_1200"],vals["cpu_temp"]); | |
mycursor.execute(sql, valin) | |
print(mycursor._executed) | |
mydb.commit() | |
print(mycursor.rowcount, "record inserted.") | |
return True | |
except mysql.connector.Error as err: | |
print(err) | |
print("Error Code:", err.errno) | |
print("SQLSTATE", err.sqlstate) | |
print("Message", err.msg) | |
return False | |
finally: | |
mycursor.close() | |
mydb.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment