update database.py and main.py

This commit is contained in:
ben
2026-08-02 20:44:38 +02:00
parent 4541d5627d
commit 8ed7993573
2 changed files with 106 additions and 3 deletions
+77
View File
@@ -0,0 +1,77 @@
import sqlite3
from pathlib import Path
DB_PATH = Path(__file__).parent.parent / "database" / "calendar.db"
def get_connection():
"""
Maakt een verbinding met de SQLite database.
"""
conn = sqlite3.connect(DB_PATH)
# Zorgt dat foreign keys werken in SQLite
conn.execute("PRAGMA foreign_keys = ON")
# Resultaten kunnen als dictionary worden gebruikt
conn.row_factory = sqlite3.Row
return conn
def execute(query, params=()):
"""
Voor INSERT, UPDATE, DELETE.
"""
with get_connection() as conn:
cursor = conn.cursor()
cursor.execute(query, params)
conn.commit()
return cursor.lastrowid
def fetch_one(query, params=()):
"""
Haalt één resultaat op.
"""
with get_connection() as conn:
cursor = conn.cursor()
cursor.execute(query, params)
return cursor.fetchone()
def fetch_all(query, params=()):
"""
Haalt meerdere resultaten op.
"""
with get_connection() as conn:
cursor = conn.cursor()
cursor.execute(query, params)
return cursor.fetchall()
def test_connection():
"""
Test of de database bereikbaar is.
"""
with get_connection() as conn:
cursor = conn.cursor()
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
return cursor.fetchall()
if __name__ == "__main__":
print("Database:", DB_PATH)
tables = test_connection()
if tables:
print("Gevonden tabellen:")
for table in tables:
print("-", table["name"])
else:
print("Geen tabellen gevonden.")
+28 -2
View File
@@ -2,5 +2,31 @@
# We had the problem of never agreeing when or where # We had the problem of never agreeing when or where
# we should meet up, and I hope this fixes the problem. # we should meet up, and I hope this fixes the problem.
import fastapi from fastapi import FastAPI
import time from database import fetch_all
app = FastAPI(
title="Calendar API",
description="Room based agenda system",
version="0.1"
)
@app.get("/")
def home():
return {
"status": "online",
"service": "Calendar API"
}
@app.get("/database")
def database_status():
tables = fetch_all(
"SELECT name FROM sqlite_master WHERE type='table';"
)
return {
"tables": [table["name"] for table in tables]
}