This commit is contained in:
Daniel Ledda
2020-11-04 18:08:42 +01:00
parent fccba1e4d7
commit cc8cd4bbc4
3 changed files with 54 additions and 14 deletions

View File

@@ -24,4 +24,31 @@ func CloseDb() {
fmt.Println("error closing database: " + err.Error())
}
}
}
func writeSnapshotToDb(snapshotSub *SnapshotSubmission) error {
query := fmt.Sprintf("INSERT INTO `snapshots` (`temp`, `humidity`, `co2`, `time`, `id`) VALUES (%v, %v, %v, '%v', NULL);",
snapshotSub.Temp,
snapshotSub.Humidity,
snapshotSub.Co2,
snapshotSub.Timestamp)
_, err := ClimateDb.Query(query)
if err != nil {
return err
}
return nil
}
func getSnapshotRecordsFromDb(rowCount int) ([]*SnapshotRecord, error) {
records := make([]*SnapshotRecord, rowCount)
query := fmt.Sprintf("SELECT * FROM `snapshots` ORDER BY `id` DESC LIMIT %v;", rowCount)
rows, err := ClimateDb.Query(query)
if err != nil {
return nil, err
}
err = rows.Scan(records)
if err != nil {
return nil, err
}
return records, nil
}