129 lines
5.4 KiB
C#
129 lines
5.4 KiB
C#
using Microsoft.Data.Sqlite;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using AquaCare.Model; // Model-Wert verwenden
|
|
|
|
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,
|
|
Gefuettert INTEGER NOT NULL,
|
|
FilterReinigen INTEGER NOT NULL,
|
|
WasserWechseln INTEGER 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(Wert wert)
|
|
{
|
|
using (var connection = new SqliteConnection(ConnectionString))
|
|
{
|
|
connection.Open();
|
|
|
|
string insertCommand = @"
|
|
INSERT INTO Werte
|
|
(Datum, Gefuettert, FilterReinigen, WasserWechseln, Temperatur, PHWert, Nitrat, Ammoniak)
|
|
VALUES
|
|
(@Datum, @Gefuettert, @FilterReinigen, @WasserWechseln, @Temperatur, @PHWert, @Nitrat, @Ammoniak);";
|
|
|
|
using (var command = new SqliteCommand(insertCommand, connection))
|
|
{
|
|
command.Parameters.AddWithValue("@Datum", wert.Datum.ToString("yyyy-MM-dd"));
|
|
command.Parameters.AddWithValue("@Gefuettert", wert.Gefuettert ? 1 : 0);
|
|
command.Parameters.AddWithValue("@FilterReinigen", wert.FilterReinigen ? 1 : 0);
|
|
command.Parameters.AddWithValue("@WasserWechseln", wert.WasserWechseln ? 1 : 0);
|
|
command.Parameters.AddWithValue("@Temperatur", wert.Temperatur);
|
|
command.Parameters.AddWithValue("@PHWert", wert.PhWert);
|
|
command.Parameters.AddWithValue("@Nitrat", wert.Nitrat);
|
|
command.Parameters.AddWithValue("@Ammoniak", wert.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, Gefuettert, FilterReinigen, WasserWechseln, 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)}, Gefüttert: {(reader.GetInt32(1) == 1 ? "Ja" : "Nein")}, " +
|
|
$"Filter gereinigt: {(reader.GetInt32(2) == 1 ? "Ja" : "Nein")}, Wasser gewechselt: {(reader.GetInt32(3) == 1 ? "Ja" : "Nein")}, " +
|
|
$"Temperatur: {reader.GetDouble(4)}°C, pH-Wert: {reader.GetDouble(5)}, Nitrat: {reader.GetDouble(6)} mg/L, Ammoniak: {reader.GetDouble(7)} mg/L";
|
|
values.Add(value);
|
|
}
|
|
}
|
|
}
|
|
|
|
return values;
|
|
}
|
|
|
|
// Ruft alle gespeicherten Werte aus der Datenbank ab und gibt sie als Wert-Objekte zurück
|
|
public List<Wert> GetValuesAsObjects()
|
|
{
|
|
var werte = new List<Wert>();
|
|
|
|
using (var connection = new SqliteConnection(ConnectionString))
|
|
{
|
|
connection.Open();
|
|
|
|
string selectCommand = "SELECT Datum, Gefuettert, FilterReinigen, WasserWechseln, 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 = DateTime.Parse(reader.GetString(0)),
|
|
Gefuettert = reader.GetInt32(1) == 1,
|
|
FilterReinigen = reader.GetInt32(2) == 1,
|
|
WasserWechseln = reader.GetInt32(3) == 1,
|
|
Temperatur = reader.GetDouble(4),
|
|
PhWert = reader.GetDouble(5),
|
|
Nitrat = reader.GetDouble(6),
|
|
Ammoniak = reader.GetDouble(7)
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
return werte;
|
|
}
|
|
}
|
|
} |