127 lines
4.6 KiB
C#
127 lines
4.6 KiB
C#
using Microsoft.Data.Sqlite;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace AquaCare.Persistence
|
|
{
|
|
public class Datenbank
|
|
{
|
|
private const string ConnectionString = "Data Source=AquaCare.db";
|
|
|
|
// Initialisiert die Datenbank und erstellt die Tabelle, falls sie nicht existiert
|
|
public void InitializeDatabase()
|
|
{
|
|
using (var connection = new SqliteConnection(ConnectionString))
|
|
{
|
|
connection.Open();
|
|
|
|
string tableCommand = @"
|
|
CREATE TABLE IF NOT EXISTS Werte (
|
|
ID INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
Datum TEXT NOT NULL,
|
|
Temperatur REAL NOT NULL,
|
|
PHWert REAL NOT NULL,
|
|
Nitrat REAL NOT NULL,
|
|
Ammoniak REAL NOT NULL
|
|
);";
|
|
|
|
using (var command = new SqliteCommand(tableCommand, connection))
|
|
{
|
|
command.ExecuteNonQuery();
|
|
}
|
|
}
|
|
}
|
|
|
|
// Fügt einen neuen Wert in die Datenbank ein
|
|
public void AddValue(DateTime datum, double temperatur, double phWert, double nitrat, double ammoniak)
|
|
{
|
|
using (var connection = new SqliteConnection(ConnectionString))
|
|
{
|
|
connection.Open();
|
|
|
|
string insertCommand = @"
|
|
INSERT INTO Werte (Datum, Temperatur, PHWert, Nitrat, Ammoniak)
|
|
VALUES (@Datum, @Temperatur, @PHWert, @Nitrat, @Ammoniak);";
|
|
|
|
using (var command = new SqliteCommand(insertCommand, connection))
|
|
{
|
|
command.Parameters.AddWithValue("@Datum", datum.ToString("yyyy-MM-dd"));
|
|
command.Parameters.AddWithValue("@Temperatur", temperatur);
|
|
command.Parameters.AddWithValue("@PHWert", phWert);
|
|
command.Parameters.AddWithValue("@Nitrat", nitrat);
|
|
command.Parameters.AddWithValue("@Ammoniak", ammoniak);
|
|
|
|
command.ExecuteNonQuery();
|
|
}
|
|
}
|
|
}
|
|
|
|
// Ruft alle gespeicherten Werte aus der Datenbank ab und gibt sie als Strings zurück
|
|
public List<string> GetValues()
|
|
{
|
|
var values = new List<string>();
|
|
|
|
using (var connection = new SqliteConnection(ConnectionString))
|
|
{
|
|
connection.Open();
|
|
|
|
string selectCommand = "SELECT Datum, Temperatur, PHWert, Nitrat, Ammoniak FROM Werte;";
|
|
|
|
using (var command = new SqliteCommand(selectCommand, connection))
|
|
using (var reader = command.ExecuteReader())
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
string value = $"Datum: {reader.GetString(0)}, Temperatur: {reader.GetDouble(1)}°C, " +
|
|
$"pH-Wert: {reader.GetDouble(2)}, Nitrat: {reader.GetDouble(3)} mg/L, " +
|
|
$"Ammoniak: {reader.GetDouble(4)} mg/L";
|
|
values.Add(value);
|
|
}
|
|
}
|
|
}
|
|
|
|
return values;
|
|
}
|
|
|
|
// Ruft alle gespeicherten Werte aus der Datenbank ab und gibt sie als Objekte zurück
|
|
public List<Wert> GetValuesAsObjects()
|
|
{
|
|
var werte = new List<Wert>();
|
|
|
|
using (var connection = new SqliteConnection(ConnectionString))
|
|
{
|
|
connection.Open();
|
|
|
|
string selectCommand = "SELECT Datum, Temperatur, PHWert, Nitrat, Ammoniak FROM Werte ORDER BY Datum ASC;";
|
|
|
|
using (var command = new SqliteCommand(selectCommand, connection))
|
|
using (var reader = command.ExecuteReader())
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
werte.Add(new Wert
|
|
{
|
|
Datum = reader.GetString(0),
|
|
Temperatur = reader.GetDouble(1),
|
|
PHWert = reader.GetDouble(2),
|
|
Nitrat = reader.GetDouble(3),
|
|
Ammoniak = reader.GetDouble(4)
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
return werte;
|
|
}
|
|
}
|
|
|
|
// Klasse zur Darstellung eines Wertes
|
|
public class Wert
|
|
{
|
|
public string Datum { get; set; }
|
|
public double Temperatur { get; set; }
|
|
public double PHWert { get; set; }
|
|
public double Nitrat { get; set; }
|
|
public double Ammoniak { get; set; }
|
|
}
|
|
} |