Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python error
#11
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.

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()
Reply


Messages In This Thread
python error - by Prashant93 - Aug-12-2018, 03:49 AM
RE: python error - by buran - Aug-12-2018, 05:48 AM
RE: python error - by Prashant93 - Aug-12-2018, 04:12 PM
RE: python error - by buran - Aug-12-2018, 04:48 PM
RE: python error - by Prashant93 - Aug-12-2018, 05:37 PM
RE: python error - by buran - Aug-12-2018, 06:07 PM
RE: python error - by Prashant93 - Aug-12-2018, 06:12 PM
RE: python error - by buran - Aug-12-2018, 06:15 PM
RE: python error - by buran - Aug-12-2018, 06:16 PM
RE: python error - by Prashant93 - Aug-12-2018, 08:40 PM
RE: python error - by Prashant93 - Aug-13-2018, 01:07 AM
RE: python error - by Prashant93 - Aug-13-2018, 07:19 PM
RE: python error - by buran - Aug-13-2018, 07:22 PM
RE: python error - by buran - Aug-13-2018, 07:30 PM
RE: python error - by Prashant93 - Aug-13-2018, 07:41 PM
RE: python error - by buran - Aug-13-2018, 07:52 PM
RE: python error - by Prashant93 - Aug-13-2018, 08:59 PM
RE: python error - by buran - Aug-14-2018, 06:22 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020