Aug-13-2018, 01:07 AM
Hey guys, I am getting the sensor readings as output but I want to store into sqlite database where I am able to create table but couldn't insert output data of sensors into my database. Please help.
Sorry this is my code:
import time import grovepi import math dbname='sensorsData.db' import sqlite3 import sys sampleFreq=1*60 #def getDHTdata(): sensor=4 blue=0 # DHTpin=16 conn=sqlite3.connect('sensorsData.db') curs=conn.cursor() while True: try: [temp,humidity]= grovepi.dht(sensor, blue) if math.isnan(temp)==False and math.isnan(humidity)==False: print("temp=%.02f C humidity=%.02f%%"%(temp, humidity)) except IOError: print ("Error") #def logData (temp,hum): sql="insert into table_name(timestamp, temp, hum) values (1, 2, 3);" cursor.execute(sql) db.commit() #curs.execute("INSERT INTO DHT_data values(datetime('now'), (temp), (hum))", (1$ #con.commit() #conn.close() print ("\nEntire database contents:\n") sql="insert into table_name(timestamp, temp, hum) values (now, temp, hum);" cursor.execute(sql) db.commit() #curs.execute("INSERT INTO DHT_data values(datetime('now'), (temp), (hum))", (1$ #con.commit() #conn.close() print ("\nEntire database contents:\n") for row in curs.execute("SELECT* FROM DHT_data"): print (row) conn.close()
Sorry this is my code:
import time import grovepi import math dbname='sensorsData.db' import sqlite3 import sys sampleFreq=1*60 #def getDHTdata(): sensor=4 blue=0 # DHTpin=16 conn=sqlite3.connect('sensorsData.db') curs=conn.cursor() while True: try: [temp,humidity]= grovepi.dht(sensor, blue) if math.isnan(temp)==False and math.isnan(humidity)==False: print("temp=%.02f C humidity=%.02f%%"%(temp, humidity)) except IOError: print ("Error") #def logData (temp,hum): curs.execute("INSERT INTO DHT_data values(datetime('now'), (temp), (hum))", (11$ con.commit() conn.close() print ("\nEntire database contents:\n") for row in curs.execute("SELECT* FROM DHT_data"): print (row) conn.close()