I am attempting to create a program that reads 3 sensors from a raspberry pi sense hat, and insert those values into a sql database, using python. When I execute the program, no errors are given, but when I try to select the data using python, or even going into the database itself, the data is nowhere to be found, I believe the data isn't being properly written. How would I go about correcting this?
import sqlite3
import datetime
from sense_hat import SenseHat
sense = SenseHat()
sensePressure = int(sense.get_pressure())
senseTemp = int(sense.get_temperature() * 1.8 + 32 - 18.85)
senseHumidity = int(sense.get_humidity())
currDateTime = datetime.datetime.now()
COUNT = 1
conn = sqlite3.connect('sense.db')
c = conn.cursor()
c.execute("INSERT INTO atmos (ITEM, DATETIME, TEMPERATURE, HUMIDITY, PRESSURE) VALUES (?, ?, ?, ?,
?)", (COUNT, currDateTime, senseTemp, sensePressure, senseHumidity))
conn.commit()
conn.close()
1 Answer 1
If you are writing to any SQL database, you will be better of using the pyodbc library it is quite effective and very easy to use :
import pyodbc
connection = pyodbc.connect('Driver={SQL Server};'
'Server=server_name;'
'Database=db_name;'
'Trusted_Connection=yes;')
cursor = connection.cursor()
cursor.execute("INSERT INTO atmos (ITEM, DATETIME, TEMPERATURE, HUMIDITY, PRESSURE)
VALUES (?, ?, ?, ?, ?)", COUNT, currDateTime, senseTemp,
sensePressure, senseHumidity)
answered May 17, 2020 at 18:09
Umar Aftab
5354 silver badges26 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
default