ATS-Doku/database.py

78 lines
2.0 KiB
Python
Raw Normal View History

2026-01-21 22:23:19 +01:00
import sqlite3
DB_NAME = "ats_doku.db"
2026-01-21 22:23:19 +01:00
def initialize_db():
connection = sqlite3.connect(DB_NAME)
2026-01-21 22:23:19 +01:00
cursor = connection.cursor()
# --- Tabelle 1: Die Liste für dein Dropdown ---
cursor.execute('''
CREATE TABLE IF NOT EXISTS ats (
2026-01-21 22:23:19 +01:00
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT UNIQUE
)
''')
# --- Tabelle 2: Das Logbuch (jetzt mit Ort) ---
cursor.execute('''
CREATE TABLE IF NOT EXISTS einsaetze (
2026-01-21 22:23:19 +01:00
id INTEGER PRIMARY KEY AUTOINCREMENT,
location TEXT,
2026-01-21 22:23:19 +01:00
name TEXT,
date TEXT,
time INTEGER
2026-01-21 22:23:19 +01:00
)
''')
# Optional: Ein paar Test-ats anlegen, falls die Tabelle leer ist
2026-01-21 22:23:19 +01:00
# (Damit du direkt was im Dropdown siehst)
cursor.execute("SELECT count(*) FROM ats")
2026-01-21 22:23:19 +01:00
if cursor.fetchone()[0] == 0:
ats_liste = [("Tim Grubmüller",), ("Phil Langer",), ("Max Hämmerle",)]
cursor.executemany("INSERT INTO ats (name) VALUES (?)", ats_liste)
2026-01-21 22:23:19 +01:00
connection.commit()
connection.close()
def add_data_to_einsaetze(name, location, date, time):
connection = sqlite3.connect(DB_NAME)
cursor = connection.cursor()
sql_query = "INSERT INTO einsaetze (name, ort, datum, zeit) VALUES (?, ?, ?, ?)"
data = (name, location, date, time)
cursor.execute(sql_query, data)
print("DB geschrieben")
connection.commit()
connection.close()
def get_ats_names():
connection = sqlite3.connect(DB_NAME)
cursor = connection.cursor()
# nur die Spalte 'name' aus der Tabelle 'ats'
cursor.execute("SELECT name FROM ats ORDER BY name ASC")
names = cursor.fetchall()
connection.close()
return [row[0] for row in names]
def get_einsaetze():
connection = sqlite3.connect(DB_NAME)
cursor = connection.cursor()
#Get Data an Row Names
connection.row_factory = sqlite3.Row
cursor.execute("SELECT * FROM einsaetze ORDER BY id DESC")
data = cursor.fetchall()
connection.commit()
connection.close()
return [dict(row) for row in data]