Working
This commit is contained in:
27
Database.go
27
Database.go
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user